I need to check if a process is running.
I have this code for this:
···
pid="2212"
cmd=“ps -f p “+pid
out=#{cmd}
lines=out.split(/\n/)
if (lines.length==1) then
print “Process with PID=”+pid+” doesn’t running.\n"
end
if (lines.length==2) then
print “Process with PID=”+pid+” is running.\n"
end
Is there any other method for this?
Is there any possibility to check this directly with ruby?
I need to check if a process is running.
I have this code for this:
pid=“2212”
cmd=“ps -f p “+pid
out=#{cmd}
lines=out.split(/\n/)
if (lines.length==1) then
print “Process with PID=”+pid+” doesn’t running.\n”
end
if (lines.length==2) then
print “Process with PID=”+pid+" is running.\n"
end
Is there any other method for this?
Is there any possibility to check this directly with ruby?
Thanks,
Gian Paolo
require “sys/proctable”
include Sys
ProcTable.ps{ |p|
if p.pid == 2212
puts “Process [” + p.pid.to_s + “] is running”
end
}
or
if ProcTable.ps(2212).empty?
puts “Process not running”
end
sys-proctable is available on the RAA. It currently supports Linux,
Solaris and FreeBSD. Windows support on the way.
Is there any other method for this?
Is there any possibility to check this directly with ruby?
Well, you can try to send the signal 0, ruby will raise the errors
Errno::ESRCH (process don't exist), Errno::EPERM (you don't have
permission) otherwise the process is present
I need to check if a process is running.
I have this code for this:
pid=“2212”
cmd=“ps -f p “+pid
out=#{cmd}
lines=out.split(/\n/)
if (lines.length==1) then
print “Process with PID=”+pid+” doesn’t running.\n”
end
if (lines.length==2) then
print “Process with PID=”+pid+" is running.\n"
end
Is there any other method for this?
Is there any possibility to check this directly with ruby?
Process.kill(0, pid):
(kill with a signal of 0 sees if the process is still alive)
irb(main):001:0> Process.kill(0,$$)
1
irb(main):002:0> Process.kill(0,$$+1)
Errno::ESRCH: No such process
from (irb):2:in `kill’
from (irb):2
irb(main):003:0> exit