Check whether a process is running (actually, present and alive) on a particular machine and/or system using its PID ...

Quick version ...


module Process
class << self
def process_alive?(pid)
begin
Process.kill(0, pid)
true
rescue Errno::ESRCH
false
end
end
end
end


A little bit slower version using /proc file system lookup ...


module Process
class << self
def process_alive?(pid)
Dir['/proc/[0-9]*'].map { |i| i.match(/\d+/)[0].to_i }.include?(pid)
end
end
end


Simple use-case:


irb(main):013:0> puts `ps aux | grep -i [t]omboy`
1000 26862 0.0 0.2 103972 9592 ? Sl Sep28 0:09 mono /usr/lib/tomboy/Tomboy.exe
=> nil
irb(main):014:0> Process.process_alive?(26862)
=> true
irb(main):015:0> Process.process_alive?(12345)
=> false
irb(main):016:0>

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/sjuGmBNkEx0/12347