Bash check if process is running or not on Linux / Unix


I am new to Linux shell scripting. I am using a CentOS 7 Linux and Ubuntu 18.04 LTS server in the cloud. How do I check if a process is running or not on Linux? How do I determine whether a process is running or not in a shell script running on an Ubuntu server?

Introduction: You can quickly determine if a process is running or not using bash shell and shell scripts. There are various command Linux and Unix command line utilities to check if the program is running with the bash shell. One can glue together a shell script and use bash shell conditional to take certain actation such as restart the process or notify sysadmin via email alert.

Bash check if process is running or not

Bash commands to check running process:

  1. pgrep command – Looks through the currently running bash processes on Linux and lists the process IDs (PID) on screen.
  2. pidof command – Find the process ID of a running program on Linux or Unix-like system
  3. ps command – Get information about the currently running Linux or Unix processes, including their process identification numbers (PIDs).

Let us see some examples.

What is a Linux or Unix process?

A Linux process is nothing but an executing (i.e., running) instance of a program. For example, Apache or Nginx web server runs on Linux or Unix-like system to display web pages in the background. All running process in the background is called as Daemon. So Apache/Nginx is a class of processes that run continuously in the background, and we say nginx or httpd daemon is running on the server. However, how do you verify that Nginx or HTTPD is running? You need to use the commands.

Is nginx process is running or not?

Type the following pgrep command:
pgrep nginx
pgrep httpd
pgrep -x mysqld

Bash determine whether a process is running or not
If the process is running you see the output on the screen; otherwise, it is not.

Bash check process running with pidof command

The syntax is:
pidof program
pidof httpd
pidof mysqld
pidof nginx

Bash shell check if a process is running or not with ps

Again the syntax is:
ps -C daemon
ps -C nginx
ps -C httpd

It is common to use the grep command or egrep command with ps as follows:
ps aux | grep nginx
ps aux | egrep -i "(nginx|httpd)"

Determine whether a process is running or not using a shell script

Each Linux or Unix bash shell command returns a status when it terminates normally or abnormally. You can use command exit status in the shell script to display an error message or take some sort of action. You can use special shell variable called $? to get the exit status of the previously executed command. To print ? variable use the echo command:
pgrep -x mysqld
echo $?
pgrep -x nginx
echo $?
pidof httpd
echo $?
ps -C httpd
echo $?

Bash check if process is running with exit status
A 0 exit status means the command was successful without any errors. A non-zero (1-255 values) exit status means command was failure.

Linux/Unix bash command to determine if process is running

It is now easy to check if the process was found or not using exit status value:

How to check if process is running in linux bash shell
Click to enlarge

Bash shell script to check running process

Bash statement allows to make choice based on the success or failure of a command:


You learned how to determine whether a process is running or not and use a conditional shell script to start/stop process based on that condition.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


Facebook Comments

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button