Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > launching process and keeping track of pid

Reply
Thread Tools

launching process and keeping track of pid

 
 
Joe Van Dyk
Guest
Posts: n/a
 
      06-25-2005
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


 
Reply With Quote
 
 
 
 
Ara.T.Howard
Guest
Posts: n/a
 
      06-25-2005
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
================================================== =============================



 
Reply With Quote
 
 
 
 
Joe Van Dyk
Guest
Posts: n/a
 
      06-25-2005
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 pts
attr id
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


 
Reply With Quote
 
Ara.T.Howard
Guest
Posts: n/a
 
      06-25-2005
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 pts
> attr id
> 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
================================================== =============================



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How to get a PID of a child process from a process openden with Popen() Miki Tebeka Python 2 04-08-2011 10:35 PM
How to get a PID of a child process from a process openden with Popen() P.S. Python 0 04-08-2011 10:40 AM
Launching a process (exe) using the same process id as the launcher franckspike C++ 1 07-03-2008 10:30 PM
Keeping track of subclasses and instances? Karlo Lozovina Python 12 10-11-2007 05:13 PM
Keeping track of which user controls need to be loaded and which not John ASP .Net 0 07-08-2003 09:26 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57