I need a platform independent (Linux/Unix|OSX) shell/bash command that will determine if a specific process is running. e.g. mysqld, httpd…
What is the simplest way/command to do this?
I need a platform independent (Linux/Unix|OSX) shell/bash command that will determine if a specific
Share
While
pidofandpgrepare great tools for determining what’s running, they are both, unfortunately, unavailable on some operating systems. A definite fail safe would be to use the following:ps cax | grep commandThe output on Gentoo Linux:
The output on OS X:
On both Linux and OS X, grep returns an exit code so it’s easy to check if the process was found or not:
Furthermore, if you would like the list of PIDs, you could easily grep for those as well:
Whose output is the same on Linux and OS X:
The output of the following is an empty string, making this approach safe for processes that are not running:
This approach is suitable for writing a simple empty string test, then even iterating through the discovered PIDs.
You can test it by saving it to a file (named “running”) with execute permissions (chmod +x running) and executing it with a parameter:
./running "httpd"WARNING!!!
Please keep in mind that you’re simply parsing the output of
ps axwhich means that, as seen in the Linux output, it is not simply matching on processes, but also the arguments passed to that program. I highly recommend being as specific as possible when using this method (e.g../running "mysql"will also match ‘mysqld’ processes). I highly recommend usingwhichto check against a full path where possible.References:
http://linux.about.com/od/commands/l/blcmdl1_ps.htm
http://linux.about.com/od/commands/l/blcmdl1_grep.htm