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?
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
#
# 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
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?
If all you need is to see if it's still running, and know when it exits, then you don't actually need its PID, you can just use a thread:
# This thread will exit when the subprogram exits
th = Thread.new { system "sleep 60" }
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 :opts
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
···
On 6/24/05, Ara.T.Howard <Ara.T.Howard@noaa.gov> 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
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
basically it just yanks out all the hashes as options with later hashes
over-riding earlier ones... not important at all
-a
···
On Sat, 25 Jun 2005, Joe Van Dyk wrote:
On 6/24/05, Ara.T.Howard <Ara.T.Howard@noaa.gov> 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 :opts
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
--
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