Crontab avoids repeated tests that have not been executed within the cycle. crontab instance
If a script is running for 30 minutes, you can set the script interval to at least one hour in Crontab to avoid conflicts. The worst case is that the script is not completed within the execution cycle, and the second script starts to run again. How can we ensure that only one script instance is running? A good method is to use lockf (lockf in FreeBSD 8.1 and flock in CentOS 5.5) to check whether a file lock can be obtained before the script is executed to prevent script running conflicts.
The lockf parameters are as follows:
-K: Always waiting to get the file lock.
-S: silent. No information is sent, even if no file lock is obtained.
-T seconds: Set the timeout time to seconds. If the time is exceeded, the system automatically gives up.
Flock-xn my. lock command
My. lock is a file and should be any file. You can create an empty file.
After the flock gets the lock, it will execute the subsequent command
Crontab example:
30 23 * * * flock -xn /tmp/my.lock -c '/usr/local/php test.php'*/5 * * * * /usr/bin/flock -xn /var/run/up_svn.lock -c'/scripts/up_svn.sh >/dev/null2>&1'
Test instance:
[hadoop@emr-worker-10 love]$ cat task.sh #! /bin/bashecho hahasleep 10echo hhheee
Terminal:
[hadoop@emr-worker-10 love]$ flock -xn test.lock -c 'sh task.sh'
Crontab:
*/5 * * * * flock -xn /tmp/my.lock -c 'sh /home/hadoop/nisj/love/task.sh'