Many of the shell scripts executed with Cron may take a long time to execute for a variety of reasons, and it is necessary to check whether they are still running while running. This article provides a Linux shell script to check with the command sh ... To execute the shell script. To make a unique check on something else, you can modify the source code a little bit.
Copy Code code as follows:
# Check whether the shell script executed through the SH command is still in execution and avoid repeated execution.
# put this code on the head of the program that needs to be guaranteed to be unique.
# Note that if you put this script directly into Cron, you have to grep-v "-C sh" to eliminate the cron-generated process!
Checkitem= "$"
Let proccnt= ' ps-a--format= '%p%p%c%x%a '--width 2048-w--sort pid|grep "$checkitem" |grep-v grep|grep-v "-C sh" |grep -V "$$" | Grep-c Sh|awk ' {printf ('%d ', '} '} '
If [${proccnt}-GT 0]; Then
The echo "$ script is already running [procs=${proccnt}] and this execution is canceled automatically."
Exit 1;
Fi