popen, popen3, and leftover processes

Discussion in 'Ruby' started by Hal Fulton, Apr 28, 2004.

  1. Hal Fulton

    Hal Fulton Guest

    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
    Hal Fulton, Apr 28, 2004
    #1
    1. Advertising

  2. Hal Fulton

    Jason Wold Guest

    >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 <> 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, Apr 28, 2004
    #2
    1. Advertising

  3. 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
    Gregory Millam, Apr 28, 2004
    #3
  4. Hal Fulton

    Jason Wold Guest

    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 <> 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 <> 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, Apr 28, 2004
    #4
  5. Hal Fulton

    Hal Fulton Guest

    Jason Wold wrote:

    >>io.pid


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

    Hal
    Hal Fulton, Apr 28, 2004
    #5
  6. Hal Fulton

    Hal Fulton Guest

    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
    Hal Fulton, Apr 28, 2004
    #6
  7. Hal Fulton

    Hal Fulton Guest

    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
    Hal Fulton, Apr 28, 2004
    #7
  8. Hal Fulton

    Hal Fulton Guest

    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
    Hal Fulton, Apr 28, 2004
    #8
  9. Hal Fulton

    Guest

    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
    , Apr 28, 2004
    #9
  10. Received: Wed, 28 Apr 2004 16:43:35 +0900
    And lo, wrote:

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

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

    # ps aux with irb still running shows no fortune.

    Thanks, good to know.
    Gregory Millam, Apr 28, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrea Crotti

    leftover pyc files

    Andrea Crotti, Nov 2, 2011, in forum: Python
    Replies:
    0
    Views:
    213
    Andrea Crotti
    Nov 2, 2011
  2. Andrea Crotti

    Re: leftover pyc files

    Andrea Crotti, Nov 2, 2011, in forum: Python
    Replies:
    21
    Views:
    576
    Steven D'Aprano
    Nov 4, 2011
  3. File.popen/IO.popen

    , May 20, 2006, in forum: Ruby
    Replies:
    1
    Views:
    214
    Robert Klemme
    May 20, 2006
  4. Marc Heiler
    Replies:
    1
    Views:
    161
    Robert Klemme
    May 24, 2009
  5. Dylan Lukes

    Subprocessing with popen or popen3?

    Dylan Lukes, Sep 18, 2009, in forum: Ruby
    Replies:
    2
    Views:
    105
    Space Ship Traveller
    Sep 19, 2009
Loading...

Share This Page