Suppose you have a script named XYZ.sh.
Inside this script you want to make sure that you run this script only if the same script is not already running. Here is how you can do this in bash shell:
Important: This method works only when the script is started as "sh XYZ.sh" and doesn't work if you start it as "./XYZ.sh"
Inside this script you want to make sure that you run this script only if the same script is not already running. Here is how you can do this in bash shell:
# find own PID using $$
echo "My proces id is : $$"
# find list of PID with process name/parameter as XYZ.sh, and remove grep's PID, own PID
prev_pid=`ps -ef | grep
XYZ .sh |grep -v grep | grep -v $$ | awk '{ print $2 }'`
if [[ $prev_pid != "" ]];then
if [[ $prev_pid != "" ]];then
echo " Script is already running with PID: [$prev_pid], so quitting now."
exit 1;
fi;
exit 1;
fi;
Important: This method works only when the script is started as "sh XYZ.sh" and doesn't work if you start it as "./XYZ.sh"
No comments:
Post a Comment