launching process and keeping track of pid

Discussion in 'Ruby' started by Joe Van Dyk, Jun 25, 2005.

  1. Joe Van Dyk

    Joe Van Dyk Guest

    Hi,

    How can I launch a program from Ruby and monitor it to see if it's
    running? On a unix platform.

    I assume I'd probably need to launch the program, capture the pid of
    the started process somehow, and start a thread that monitors the
    /proc directory to see if the pid is in there?

    Thanks,
    Joe
     
    Joe Van Dyk, Jun 25, 2005
    #1
    1. Advertising

  2. Joe Van Dyk

    Ara.T.Howard Guest

    On Sat, 25 Jun 2005, Joe Van Dyk wrote:

    > Hi,
    >
    > How can I launch a program from Ruby and monitor it to see if it's
    > running? On a unix platform.
    >
    > I assume I'd probably need to launch the program, capture the pid of
    > the started process somehow, and start a thread that monitors the
    > /proc directory to see if the pid is in there?
    >
    > Thanks,
    > Joe


    this might be helpful:

    #
    # returns true if pid is running, false otherwise
    #
    def alive pid
    #--{{{
    pid = Integer("#{ pid }")
    begin
    Process::kill 0, pid
    true
    rescue Errno::ESRCH
    false
    end
    #--}}}
    end
    alias alive? alive

    cheers.


    -a
    --
    ===============================================================================
    | email :: ara [dot] t [dot] howard [at] noaa [dot] gov
    | phone :: 303.497.6469
    | My religion is very simple. My religion is kindness.
    | --Tenzin Gyatso
    ===============================================================================
     
    Ara.T.Howard, Jun 25, 2005
    #2
    1. Advertising

  3. Joe Van Dyk

    Joe Van Dyk Guest

    On 6/24/05, Ara.T.Howard <> wrote:
    > On Sat, 25 Jun 2005, Joe Van Dyk wrote:
    >=20
    > > Hi,
    > >
    > > How can I launch a program from Ruby and monitor it to see if it's
    > > running? On a unix platform.
    > >
    > > I assume I'd probably need to launch the program, capture the pid of
    > > the started process somehow, and start a thread that monitors the
    > > /proc directory to see if the pid is in there?
    > >
    > > Thanks,
    > > Joe

    >=20
    > this might be helpful:
    >=20
    > #
    > # returns true if pid is running, false otherwise
    > #
    > def alive pid
    > #--{{{
    > pid =3D Integer("#{ pid }")
    > begin
    > Process::kill 0, pid
    > true
    > rescue Errno::ESRCH
    > false
    > end
    > #--}}}
    > end
    > alias alive? alive
    >=20


    Thanks!=20

    I asked a question similar to this a couple months ago and you
    responded with the following code. However, my Ruby-fu isn't strong
    enough to decipher it (the optfilter and IO bit). Any chance you
    could add some comments to it?


    class BackGroundProcess
    attr :argv
    attr :eek:pts
    attr :pid
    def initialize(*args)
    @argv, @opts =3D optfilter args
    return if((@pid =3D fork))
    perform_redirects
    exec(*@argv)
    end
    def perform_redirects
    if((io =3D (opts[:stdout] or opts['stdout'])))
    STDOUT.reopen(IO =3D=3D=3D io ? io : open(io,'w'))
    end
    if((io =3D (opts[:stderr] or opts['stderr'])))
    STDERR.reopen(IO =3D=3D=3D io ? io : open(io,'w'))
    end
    end
    def optfilter list
    hashes, args =3D list.partition{|elem| Hash =3D=3D=3D elem}
    opts =3D hashes.inject(accum=3D{}){|accum, h| accum.update h}
    [ args, opts ]
    end
    end
     
    Joe Van Dyk, Jun 25, 2005
    #3
  4. Joe Van Dyk

    Ara.T.Howard Guest

    On Sat, 25 Jun 2005, Joe Van Dyk wrote:

    > On 6/24/05, Ara.T.Howard <> wrote:
    >> On Sat, 25 Jun 2005, Joe Van Dyk wrote:
    >>
    >>> Hi,
    >>>
    >>> How can I launch a program from Ruby and monitor it to see if it's
    >>> running? On a unix platform.
    >>>
    >>> I assume I'd probably need to launch the program, capture the pid of
    >>> the started process somehow, and start a thread that monitors the
    >>> /proc directory to see if the pid is in there?
    >>>
    >>> Thanks,
    >>> Joe

    >>
    >> this might be helpful:
    >>
    >> #
    >> # returns true if pid is running, false otherwise
    >> #
    >> def alive pid
    >> #--{{{
    >> pid = Integer("#{ pid }")
    >> begin
    >> Process::kill 0, pid
    >> true
    >> rescue Errno::ESRCH
    >> false
    >> end
    >> #--}}}
    >> end
    >> alias alive? alive
    >>

    >
    > Thanks!
    >
    > I asked a question similar to this a couple months ago and you
    > responded with the following code. However, my Ruby-fu isn't strong
    > enough to decipher it (the optfilter and IO bit). Any chance you
    > could add some comments to it?
    >
    >
    > class BackGroundProcess
    > attr :argv
    > attr :eek:pts
    > attr :pid
    > def initialize(*args)
    > @argv, @opts = optfilter args
    > return if((@pid = fork))
    > perform_redirects
    > exec(*@argv)
    > end
    > def perform_redirects
    > if((io = (opts[:stdout] or opts['stdout'])))
    > STDOUT.reopen(IO === io ? io : open(io,'w'))
    > end
    > if((io = (opts[:stderr] or opts['stderr'])))
    > STDERR.reopen(IO === io ? io : open(io,'w'))
    > end
    > end
    > def optfilter list
    > hashes, args = list.partition{|elem| Hash === elem}
    > opts = hashes.inject(accum={}){|accum, h| accum.update h}
    > [ args, opts ]
    > end
    > end


    optfilter just takes things that looks like options and uses them as options,
    things that are not hashes are arugments. it just let's you do things like

    options = {'stdout' => StringIO::new}

    BackGroundProcess::new 'cat', options, 'stderr' => StringIO::new

    or

    BackGroundProcess::new options, 'cat', 'stderr' => StringIO::new

    basically it just yanks out all the hashes as options with later hashes
    over-riding earlier ones... not important at all ;-)

    -a
    --
    ===============================================================================
    | email :: ara [dot] t [dot] howard [at] noaa [dot] gov
    | phone :: 303.497.6469
    | My religion is very simple. My religion is kindness.
    | --Tenzin Gyatso
    ===============================================================================
     
    Ara.T.Howard, Jun 25, 2005
    #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. John
    Replies:
    0
    Views:
    372
  2. Karlo Lozovina

    Keeping track of subclasses and instances?

    Karlo Lozovina, Oct 11, 2007, in forum: Python
    Replies:
    12
    Views:
    484
    Bruno Desthuilliers
    Oct 11, 2007
  3. franckspike
    Replies:
    1
    Views:
    338
    Boris
    Jul 3, 2008
  4. P.S.
    Replies:
    0
    Views:
    351
  5. Miki Tebeka
    Replies:
    2
    Views:
    633
    Chris Angelico
    Apr 8, 2011
Loading...

Share This Page