Kill an IO.popen'ed program?

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

···

--
Posted via http://www.ruby-forum.com/.

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)

···

--
Nobu Nakada

Jared Davis wrote:

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}")

--
Posted via http://www.ruby-forum.com/\.