From: Mark Hubbart Date: 2004-08-26T06:26:06+09:00 Subject: Re: easy way to check if a pid is running? On Aug 25, 2004, at 2:01 PM, Gennady wrote: > George Mochrie wrote: >> hi >> A quick question: >> Is there an easy way to check if a process is running given it's PID? >> There seems to be nothing like Process.exists?(pid). If I was root I >> could use Process.kill(0,pid) to test, but the program needs to run >> as a plain user. Do > > Not only root can do kill(0, pid), any user can send signal (or 0) to > his/her own processes. > > On systems that have /proc you can also check for presence of a > directory named like pid there. You can't send a signal, but you get a different error: mark@eMac% irb --prompt xmp Process.kill 0, 1 Errno::EPERM: Operation not permitted from (irb):2:in `kill' from (irb):2 Process.kill 0, 512 Errno::ESRCH: No such process from (irb):3:in `kill' from (irb):3 Process.kill 0, 419 ==>1 process 1 is owned by root, 512 doesn't exist, 419 was mine. So, Process.exists? could be: def Process.exists?(pid) Process.kill 0, pid return true rescue Errno::ESRCH return false rescue Exception return true end ==>nil Process.exists? 1 ==>true Process.exists? 512 ==>false Process.exists? 419 ==>true >> I have to parse the output from ps -e? >> Thanks in advance :) > > Gennady. >