Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux/Unix command to determine if process is running?

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?

like image 883
Highway of Life Avatar asked Feb 02 '12 18:02

Highway of Life


People also ask

How do I tell if a process is running in Linux?

You can list running processes using the ps command (ps means process status). The ps command displays your currently running processes in real-time.

How do you check if a process is running or not?

Bash commands to check running process: pgrep command – Looks through the currently running bash processes on Linux and lists the process IDs (PID) on screen. pidof command – Find the process ID of a running program on Linux or Unix-like system.

Which command is used to know if the process is running?

Any time the system is running, processes are also running. You can use the ps command to find out which processes are running and display information about those processes.


1 Answers

While pidof and pgrep are 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 command

The output on Gentoo Linux:

 14484 ?        S      0:00 apache2 14667 ?        S      0:00 apache2 19620 ?        Sl     0:00 apache2 21132 ?        Ss     0:04 apache2 

The output on OS X:

 42582   ??  Z      0:00.00 (smbclient) 46529   ??  Z      0:00.00 (smbclient) 46539   ??  Z      0:00.00 (smbclient) 46547   ??  Z      0:00.00 (smbclient) 46586   ??  Z      0:00.00 (smbclient) 46594   ??  Z      0:00.00 (smbclient) 

On both Linux and OS X, grep returns an exit code so it's easy to check if the process was found or not:

#!/bin/bash ps cax | grep httpd > /dev/null if [ $? -eq 0 ]; then   echo "Process is running." else   echo "Process is not running." fi 

Furthermore, if you would like the list of PIDs, you could easily grep for those as well:

ps cax | grep httpd | grep -o '^[ ]*[0-9]*'

Whose output is the same on Linux and OS X:

3519 3521 3523 3524

The output of the following is an empty string, making this approach safe for processes that are not running:

echo ps cax | grep aasdfasdf | grep -o '^[ ]*[0-9]*'

This approach is suitable for writing a simple empty string test, then even iterating through the discovered PIDs.

#!/bin/bash PROCESS=$1 PIDS=`ps cax | grep $PROCESS | grep -o '^[ ]*[0-9]*'` if [ -z "$PIDS" ]; then   echo "Process not running." 1>&2   exit 1 else   for PID in $PIDS; do     echo $PID   done fi 

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"

#!/bin/bash ps cax | grep httpd if [ $? -eq 0 ]; then   echo "Process is running." else   echo "Process is not running." fi 

WARNING!!!

Please keep in mind that you're simply parsing the output of ps ax which 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 using which to 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

like image 75
Caleb Gray Avatar answered Sep 17 '22 13:09

Caleb Gray