Created
February 23, 2012 07:28
-
-
Save modsaid/1891303 to your computer and use it in GitHub Desktop.
Checking if a process with a certain pid is running
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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