Getting PID of external command

Discussion in 'Ruby' started by Marc Heiler, Mar 12, 2007.

  1. Marc Heiler

    Marc Heiler Guest

    Hi there,

    I am using

    Kernel.system("mplayer foobar.wav")

    to play a song. This works, but I also need to be able
    to kill it from a script. My idea was to simply
    get the PID of the mplayer process, Kernel.system
    however does not seem to allow this easily, and
    Io.popen doesnt wait for it to have finished.

    Is there a way to get the PID?

    --
    Posted via http://www.ruby-forum.com/.
    Marc Heiler, Mar 12, 2007
    #1
    1. Advertising

  2. On Mon, Mar 12, 2007 at 11:03:09AM +0900, Marc Heiler wrote:
    > I am using
    >
    > Kernel.system("mplayer foobar.wav")
    >
    > to play a song. This works, but I also need to be able
    > to kill it from a script. My idea was to simply
    > get the PID of the mplayer process, Kernel.system
    > however does not seem to allow this easily, and
    > Io.popen doesnt wait for it to have finished.


    There's a chicken-and-egg there. You're asking for the pid to be available
    before the command terminates, but you're also asking for the command not to
    return until the command terminates :)

    Perhaps you want to invoke a callback function with the pid?

    You can implement Kernel.system() yourself very easily: it's just a fork and
    exec.

    def my_system(*cmd)
    pid = fork do
    exec(*cmd)
    exit! 127
    end
    yield pid if block_given?
    Process.waitpid(pid)
    $?
    end

    my_system("sleep 10") { |pid| puts "Pid is #{pid}" }

    But I expect IO.popen will be better in many cases, in particular if you
    want to capture the stdout of the child process. With system() and the above
    code, the child process shares stdout with the parent.

    B.
    Brian Candler, Mar 12, 2007
    #2
    1. Advertising

  3. Marc Heiler

    Marc Heiler Guest

    Thanks, I think that does what I need, at least my first test worked. :)

    Is there a way to exchange $? with a longer name? Is that $CHILD_STATUS?

    --
    Posted via http://www.ruby-forum.com/.
    Marc Heiler, Mar 17, 2007
    #3
  4. Marc Heiler schrieb:
    > Is there a way to exchange $? with a longer name? Is that $CHILD_STATUS?

    Yes, but don't forget to include English!

    require 'English'
    $CHILD_STATUS


    regards
    Jan
    Jan Friedrich, Mar 17, 2007
    #4
    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. Sunil Menon
    Replies:
    2
    Views:
    546
    Sunil Menon
    Jul 27, 2003
  2. Harlin Seritt

    Getting PID for process

    Harlin Seritt, Apr 29, 2005, in forum: Python
    Replies:
    2
    Views:
    360
    runes
    Apr 29, 2005
  3. Replies:
    1
    Views:
    607
    Gabriel Genellina
    Feb 13, 2007
  4. ma_kiem
    Replies:
    0
    Views:
    442
    ma_kiem
    Jul 6, 2011
  5. Roger Pack

    Process.waitpid(external pid)

    Roger Pack, Dec 17, 2009, in forum: Ruby
    Replies:
    2
    Views:
    154
    Roger Pack
    Dec 18, 2009
Loading...

Share This Page