popen, popen3, and leftover processes

H

Hal Fulton

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
 
J

Jason Wold

io=IO.popen("sleep 1000")
=> # said:
=> 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
 
G

Gregory Millam

Received: Wed, 28 Apr 2004 15:52:49 +0900
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
 
J

Jason Wold

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?
 
H

Hal Fulton

Gregory said:
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
 
H

Hal Fulton

Jason said:
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
 
H

Hal Fulton

Jason said:
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
 
N

nobu.nokada

Hi,

At Wed, 28 Apr 2004 16:08:58 +0900,
Hal Fulton wrote in [ruby-talk:98616]:
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.
 
G

Gregory Millam

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

At Wed, 28 Apr 2004 16:08:58 +0900,
Hal Fulton wrote in [ruby-talk:98616]:
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 <defunct>

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

# ps aux with irb still running shows no fortune.

Thanks, good to know.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top