How to check for a running process with Ruby?

This looks neater I think, and uses built-in Ruby module. Send a 0 kill signal (i.e. don't kill):

  # Check if a process is running
  def running?(pid)
    Process.kill(0, pid)
    true
  rescue Errno::ESRCH
    false
  rescue Errno::EPERM
    true
  end

Slightly amended from Quick dev tips You might not want to rescue EPERM, meaning "it's running, but you're not allowed to kill it".


unless `ps aux | grep ar_sendmai[l]` != ""

unless `pgrep -f ar_sendmail`.split("\n") != [Process.pid.to_s]