Shell Script to Check If a Process is Running

Last updated: February 17, 2024 | Bobbin Zachariah

In this article, we'll explore a shell script designed to check the status of a process based on either its process ID (PID) or name. This script provides a flexible and user-friendly way to determine whether a specific process is running.

The primary goal of this script is to determine whether a specific process is currently running on the system. This can be helpful in various scenarios, such as monitoring critical applications or ensuring that certain services are up and operational.

In the shell script, will use ps -ef and pgrep command to get the details of process is running or not with process id or name.

Bash Script to Check Process Running

Let me first show the script output (from my Ubuntu machine):

$ ./status-process-check.sh nginx
Process nginx is running with parent pid 2845357.

$ ./status-process-check.sh 2845357
Process id 2845357 is running

The script output confirms that nginx process is running. It can also check specific process ID, here from the output it confirms process ID 2845357 is running.

Script

#!/bin/sh

check_process_with_pid() {
	local process_id=$1
	cnt=`ps -ef | grep $process_id | grep -v grep | wc -l`
	if [ $cnt != 0 ] ; then
		echo "Process id $process_id is running"
	fi
}

check_process_with_name() {
	local process_name=$1
	cnt=`pgrep $process_name | wc -l`
	if [ $cnt != 0 ] ; then
		pid=`ps -ef | grep $process_name | grep -v grep | head -n1 | awk -F" " '{print $2}'`
		echo "Process $process_name is running with parent pid $pid".
	fi
}

process_id=""
process_name=""

if [ "$1" -eq "$1" ] 2>/dev/null; then
	process_id=$1
else
    process_name=$1
fi

if [ -z $process_id ] ; then
	if [ ! -z $process_name ] ; then
	#check with process_name
		check_process_with_name $process_name
	else
	#check with process_id
		check_process_with_pid $process_id
	fi
else 
	#check with process_id
	check_process_with_pid $process_id
fi

In the script:

The provided shell script is designed to check the status of a process using two functions: check_process_with_pid and check_process_with_name.

The check_process_with_pid functions take a process ID as an argument, check the running processes using ps -ef and grep, and provide relevant information if the process is found.

check_process_with_pid() {
	local process_id=$1
	cnt=`ps -ef | grep $process_id | grep -v grep | wc -l`
	if [ $cnt != 0 ] ; then
		echo "Process id $process_id is running"
	fi
}

The check_process_with_name functions take a process name as an argument, check the running processes using pgrep, and provide relevant information if the process is found.

check_process_with_name() {
	local process_name=$1
	cnt=`pgrep $process_name | wc -l`
	if [ $cnt != 0 ] ; then
		pid=`ps -ef | grep $process_name | grep -v grep | head -n1 | awk -F" " '{print $2}'`
		echo -e "Process $process_name is running with parent pid $pid".
	fi
}

Execution

These functions take either a process ID or name as an argument, and based on input will also check and will check using below statements in the script.

  • if input argument is number then will consider as process id 
  • If input argument is not number then will consider as process name

Below if statement check if given argument contains number, if yes then store it into “process_id” variable as store into “process_name” variable

if [ "$1" -eq "$1" ] 2>/dev/null; then
	process_id=$1
else
    	process_name=$1
fi

This section determines whether the user has provided a process ID or name and calls the appropriate function accordingly. It also handles scenarios where both the process ID and name are empty.

if [ -z $process_id ] ; then
	if [ ! -z $process_name ] ; then
		#check with process_name
		check_process_with_name $process_name
	else
		#check with process_id
		check_process_with_pid $process_id
	fi
else 
	#check with process_id
	check_process_with_pid $process_id
fi
SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment