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?

How to check if process is already running in bash (shell script) 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 – 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 – 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"

 

Check if process is already running – Method 3

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"

 

 

2 Comments

  1. I was very pleased to find this web-site.I wanted to thanks for your time for this wonderful read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post.

Leave a Reply

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