[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Kill an IO.popen'ed program?

Jared Davis

10/29/2007 6:34:00 AM

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-....

2 Answers

Nobuyoshi Nakada

10/29/2007 7:12:00 AM

0

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

7stud --

10/29/2007 7:43:00 AM

0

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-....