Project

General

Profile

Feature #12573

Updated by will_in_wi (William Johnston) over 8 years ago

## h2. Background 

 The best present solution is to kill the process with a signal of 0, and then evaluate the exit code and two possible exceptions to determine whether or not this process is alive. This is surprising when Ruby has so many beautiful ways of handling related things. 

 In Ruby, this looks like (from the process_exists gem): like: 

 ```ruby <pre><code class="ruby"> 
 def self.exists?(pid) 
   Process.kill(0, pid.to_i) 
   true 
 rescue Errno::ESRCH # No such process 
   false 
 rescue Errno::EPERM # The process exists, but you don't dont have permission to send the signal to it. 
   true 
 end 
 ``` </code></pre> 

 ## h2. Usecase 

 I have a background process which needs to be able to determine whether or not another copy of it is running. This is accomplished via a PID file, but I need to be able to confirm whether the given PID actually exists. 

 ## h2. See also 

 Someone has already wrapped up a Ruby version of this into a gem (from which the Ruby implementation comes): https://github.com/wilsonsilva/process_exists 

Back