Skip to content

Instantly share code, notes, and snippets.

@modsaid
Created February 23, 2012 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save modsaid/1891303 to your computer and use it in GitHub Desktop.
Save modsaid/1891303 to your computer and use it in GitHub Desktop.
Checking if a process with a certain pid is running
# Sometimes, specially if we are building a monitoring system or an admin UI,
# we need to be able to easily check if a process is running.
# Unfortunately this is not directly available through Process module. so here is a patch to add it
#
# Usage:
# Process.running?(pid)
# Process.memory(pid) # returns the memory usage in KB
module Process
def self.running?(pid)
return false if pid.nil?
begin
Process.kill(0, pid.to_i)
return true
rescue Errno::ESRCH
return false
end
end
def self.memory(pid=nil)
pid ||= Process.pid
x=`ps ax -o pid,rss | grep -E "^[[:space:]]*#{pid}"`.strip.split.last
x.to_i/1024
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment