How can I check from Ruby whether a process with a certain pid is running?

Viewed 34962

If there is more than one way, please list them. I only know of one, but I'm wondering if there is a cleaner, in-Ruby way.

9 Answers

If it's a process you expect to "own" (e.g. you're using this to validate a pid for a process you control), you can just send sig 0 to it.

>> Process.kill 0, 370
=> 1
>> Process.kill 0, 2
Errno::ESRCH: No such process
    from (irb):5:in `kill'
    from (irb):5
>> 

@John T, @Dustin: Actually, guys, I perused the Process rdocs, and it looks like

Process.getpgid( pid )

is a less violent means of applying the same technique.

You can try using

Process::kill 0, pid

where pid is the pid number, if the pid is running it should return 1.

Related