Crontab avoids repeated tests that have not been executed within the cycle. crontab instance

Source: Internet
Author: User
Tags flock crontab example

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'

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.