Kill an IO.popen'ed program?

J

Jared Davis

Hi,

I'm looking for a way to forcibly terminate a process started with a
pipe. That is, when I run:

file = IO.popen("sleep 300", "r+")
file.close

The file.close command just hangs and waits for the sleep command to
finish. Is there some way I can instead say, "I don't care if it's
finished or not, kill the program now and close the pipe"?

Thanks,
Jared
 
N

Nobuyoshi Nakada

Hi,

At Mon, 29 Oct 2007 15:33:37 +0900,
Jared Davis wrote in [ruby-talk:276378]:
I'm looking for a way to forcibly terminate a process started with a
pipe. That is, when I run:

Process.kill("TERM", file.pid)
 
7

7stud --

Jared said:
Hi,

I'm looking for a way to forcibly terminate a process started with a
pipe. That is, when I run:

file = IO.popen("sleep 300", "r+")
file.close

The file.close command just hangs and waits for the sleep command to
finish. Is there some way I can instead say, "I don't care if it's
finished or not, kill the program now and close the pipe"?

Thanks,
Jared


Try this:

p = IO.popen("sleep 10", "r+")

id = p.pid
puts id

sleep(5)
Process.kill("SIGALRM", id)


The man page for sleep says:

-----
The sleep utility exits with one of the following values:

0 On successful completion, or if the signal SIGALRM was
received.
0 An error occurred.
-----


Or you can issue a shell command using system():


#Process.kill("SIGALRM", id)
system("kill -SIGALRM #{id}")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,159
Latest member
SweetCalmCBDGummies
Top