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"
Check if process is already running (Recommended) - 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"
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
I spent around an hour reinventing the wheel before I found this article. Way better than what I came up with. Thanks.
Don’t know why, but I have had bad luck using pgrep in the past and I usually use ps -ef | grep … instead.
Hello there! Do you use Twitter? I’d like to follow you if that would
be okay. I’m undoubtedly enjoying your blog and look forward to new posts.
Hello Cierra, Thanks for your feedback. You can subscribe us or connect us at our Facebook account to get the latest updates
Very good post. I absolutely appreciate this site. Keep writing!
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.