Solved: Check if process is already running in Bash Linux

Solved: Check if process is already running in Bash Linux

Earlier I shared the steps to run shell scripts in parallel and to collect exit status of the respective process in Linux. Now How to check if process is already running in bash Linux? How to determine if multiple instances of same script or process is running in background? How to exit a script if duplicate instance of the same script is already running on another session in shell script using bash in Linux?

There are multiple methods to determine duplicate instance and to check if process is already running. I will share some of the methods i commonly use in shell scripts.


Check if process is already running using pidof - Method 1

The most simple and straight forward way to do this would be to use pidof. With the below function at the starting of your script, you can make sure only one instance of your script is running at a time.

#!/bin/bash
script_name=$(basename -- "$0")

if pidof -x "$script_name" -o $$ >/dev/null;then
   echo "An another instance of this script is already running, please clear all the sessions of this script before starting a new session"
   exit 1
fi

Check if process is already running by checking PID status- Method 2

With every system daemon which starts, a PID file is created under /var/run. So we can use similar method to track the PID status of any already running instance of your script
Now this method is not very robust, for example if the script exited abruptly for some reason before deleting the PID file from /var/run then even when there is no instance of the script in running state, still the script would throw ERROR and exit.

But it can still be useful by adding a trap and performing a cleanup for any abrupt exit.

#!/bin/bash

script_name=$(basename -- "$0")
pid=(`pgrep -f $script_name`)
[[ -z $pid ]] && echo "Failed to get the PID" && exit 1

if [ -f "/var/run/$script_name" ];then
   echo "An another instance of this script is already running, please clear all the sessions of this script before starting a new session"
   exit 1
fi

echo $pid > /var/run/$script_name

# Main Function

rm -f "/var/run/$script_name"

This method uses the function from Method 2 but is more robust. Here we will add some more checks to make sure even if the script exits abruptly, the function will perform the required cleanup.

script_name=$(basename -- "$0")
pid=(`pgrep -f $script_name`)
pid_count=${#pid[@]}

[[ -z $pid ]] && echo "Failed to get the PID"

if [ -f "/var/run/$script_name" ];then
   if [[  $pid_count -gt "1" ]];then
      echo "An another instance of this script is already running, please clear all the sessions of this script before starting a new session"
      exit 1
   else
      echo "Looks like the last instance of this script exited unsuccessfully, perform cleanup"
      rm -f "/var/run/$script_name"
   fi
fi

echo $pid > /var/run/$script_name

# Main Function

rm -f "/var/run/$script_name"

Related keywords: bash check if script is already running, bash check if process is running on port, check if process is running linux shell script, linux check if process is running and restart if not, bash if process is running kill it, bash script to check if process is running and send email, how to check when the process is stopped in linux, how to check in unix if process is running

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.