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

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

Bash determine whether a process is running or not

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


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

Bash check if process is running with exit status

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


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:

################### ## 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”
How to check if process is running in linux bash shell
Click to enlarge

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

A note about service and systemctl command

Linux systemctl and Unix service command examples

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


Source : https://www.cyberciti.biz/faq/bash-check-if-process-is-running-or-notonlinuxunix/

Posted on: December 17, 2019, by :