Popen, popen3, and leftover processes

I’m getting processes left lying around when I do things
like

io = IO.popen(“foo”) # or…
inp, out, err = Open3.popen3(“foobar”)

I’ve tried such things as closing the io object, but to
no avail.

I see no way of retrieving the pid in either case.

What can I do?

Thanks,
Hal

io=IO.popen(“sleep 1000”)
=> #IO:0x40140a8c

io.pid
=> 2676

puts ps auwx | grep sleep
ital 2676 0.0 0.1 1900 472 pts/0 S 23:58 0:00 sleep 1000
ital 2677 0.0 0.3 2048 872 pts/0 S 23:59 0:00 sh -c
ps auwx | grep sleep
ital 2679 0.0 0.1 1500 432 pts/0 S 23:59 0:00 grep sleep
=> nil

···

On Wed, 28 Apr 2004 15:52:49 +0900, Hal Fulton hal9000@hypermetrics.com wrote:

I’m getting processes left lying around when I do things
like

io = IO.popen(“foo”) # or…
inp, out, err = Open3.popen3(“foobar”)

I’ve tried such things as closing the io object, but to
no avail.

I see no way of retrieving the pid in either case.

What can I do?

Thanks,
Hal

Received: Wed, 28 Apr 2004 15:52:49 +0900
And lo, Hal wrote:

I’m getting processes left lying around when I do things
like

io = IO.popen(“foo”) # or…
inp, out, err = Open3.popen3(“foobar”)

I’ve tried such things as closing the io object, but to
no avail.

I see no way of retrieving the pid in either case.

No need for a pid

Process.wait

Ah, but I just noticed that popen3 doesn’t save the pid info on any of
the IO objects it returns. bug or missing feature?

···

On Wed, 28 Apr 2004 00:00:31 -0700, Jason Wold jason.wold@gmail.com wrote:

io=IO.popen(“sleep 1000”)
=> #IO:0x40140a8c

io.pid
=> 2676

puts ps auwx | grep sleep
ital 2676 0.0 0.1 1900 472 pts/0 S 23:58 0:00 sleep 1000
ital 2677 0.0 0.3 2048 872 pts/0 S 23:59 0:00 sh -c
ps auwx | grep sleep
ital 2679 0.0 0.1 1500 432 pts/0 S 23:59 0:00 grep sleep
=> nil

On Wed, 28 Apr 2004 15:52:49 +0900, Hal Fulton hal9000@hypermetrics.com wrote:

I’m getting processes left lying around when I do things
like

io = IO.popen(“foo”) # or…
inp, out, err = Open3.popen3(“foobar”)

I’ve tried such things as closing the io object, but to
no avail.

I see no way of retrieving the pid in either case.

What can I do?

Thanks,
Hal

Jason Wold wrote:

io.pid

:slight_smile: Thank you. After 2 a.m., my reading skills decline.

Hal

Gregory Millam wrote:

No need for a pid

Process.wait

Hmm, but I don’t want to wait for it. I want to make sure the process
dies. I think io.pid may be the way to go.

Hal

Jason Wold wrote:

Ah, but I just noticed that popen3 doesn’t save the pid info on any of
the IO objects it returns. bug or missing feature?

Well, that would be problematic.

It does seem like a missing feature to me.

Hal

Jason Wold wrote:

Ah, but I just noticed that popen3 doesn’t save the pid info on any of
the IO objects it returns. bug or missing feature?

As it turns out, adding this will fix it:

 pi.each do |x|
   class << x
     attr_accessor :pid
   end
   x.pid = pid
 end

A bit ugly, but I’ll modify my private copy this way until
I see something better…

Hal

Hi,

At Wed, 28 Apr 2004 16:08:58 +0900,
Hal Fulton wrote in [ruby-talk:98616]:

No need for a pid

Process.wait

Hmm, but I don’t want to wait for it. I want to make sure the process
dies. I think io.pid may be the way to go.

You don’t need to wait for IO.popen, popen’ed IO waits the
child process at close. However, popen3 spawns child and
grand-child process which the command is executed as, so you
cannot wait the real command. This restriction is to get rid
of leaving a zombie process.

···


Nobu Nakada

Received: Wed, 28 Apr 2004 16:43:35 +0900

Hi,

At Wed, 28 Apr 2004 16:08:58 +0900,
Hal Fulton wrote in [ruby-talk:98616]:

No need for a pid

Process.wait

Hmm, but I don’t want to wait for it. I want to make sure the process
dies. I think io.pid may be the way to go.

You don’t need to wait for IO.popen, popen’ed IO waits the
child process at close. However, popen3 spawns child and
grand-child process which the command is executed as, so you
cannot wait the real command. This restriction is to get rid
of leaving a zombie process.

Hmm. … right, I never knew that :D.

irb(main):001:0> f = IO.popen(“fortune”)
=> #IO:0x402be42c
irb(main):002:0> f.read
=> “Do not meddle in the affairs of wizards, for they become soggy and hard to\nlight.\n\nDo not throw cigarette butts in the urinal, for they are subtle and\nquick to anger.\n”

ps aux with irb running shows fortune

irb(main):003:0> f.close

ps aux with irb still running shows no fortune.

Thanks, good to know.

···

And lo, nobu.nokada@softhome.net wrote: