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

ADVERTISEMENTS



Bash check if process is running or not

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 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 ps command – Get information about the currently running Linux or Unix processes, including their process identification numbers (PIDs).

Let us see some examples about checking processes that running or not in Linux and Unix systems.

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 check process running with pidof command

If the process is running you see the output on the screen; otherwise, it is not.

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 $?



Linux/Unix bash command to determine if process is running

A 0 exit status means the command was successful without any errors. A non-zero (1-255 values) exit status means command was failure.

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

################### ## pgrep example ## ################### pgrep -x mysqld >/ dev / null && echo "Process found" || echo "Process not found" pgrep -x httpd >/ dev / null && echo "Process found" || echo "Process not found" ################### ## pidof example ## ################### pidof httpd >/ dev / null && echo "Service is running" || echo "Service NOT running" pidof nginx >/ dev / null && echo "Service is running" || echo "Service NOT running" ################ ## ps example ## ################ ps -C httpd >/ dev / null && echo "Running" || echo "Not running" ps -C nginx >/ dev / null && echo "Running" || echo "Not running" ################### ## pgrep example ## ################### pgrep -x mysqld >/dev/null && echo "Process found" || echo "Process not found" pgrep -x httpd >/dev/null && echo "Process found" || echo "Process not found" ################### ## pidof example ## ################### pidof httpd >/dev/null && echo "Service is running" || echo "Service NOT running" pidof nginx >/dev/null && echo "Service is running" || echo "Service NOT running" ################ ## ps example ## ################ ps -C httpd >/dev/null && echo "Running" || echo "Not running" ps -C nginx >/dev/null && echo "Running" || echo "Not running"

Bash shell script to check running process

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

#!/bin/bash SERVICE = "nginx" if pgrep -x " $SERVICE " >/ dev / null then echo " $SERVICE is running" else echo " $SERVICE stopped" # uncomment to start nginx if stopped # systemctl start nginx # mail fi #!/bin/bash SERVICE="nginx" if pgrep -x "$SERVICE" >/dev/null then echo "$SERVICE is running" else echo "$SERVICE stopped" # uncomment to start nginx if stopped # systemctl start nginx # mail fi

A note about service and systemctl command

One can use systemctl command to control the systemd system under Linux. It can provide status of service too. For example, find out if nginx is running or out, run:

systemctl status {service}

systemctl status sshd

systemctl status nginx

Older Linux distros and Unix like system such as FreeBSD use service command for the same purpose. The syntax is:

sudo service {service} status

sudo service nginx status

sudo service sshd status



Conclusion

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. See pgrep and bash man page here for more information.