Crontab Troubleshooting Scripting Conflicts

Source: Internet
Author: User

Linux crontab is a good thing, but its default minimum execution frequency is 1 minutes, but we in the actual production environment sometimes encountered the script execution time is more than 1 minutes, so there is a very embarrassing situation, that is, after 1 minutes, the system process will appear multiple scripts, Neck and neck run in the background, such as this:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/8C/92/wKioL1hwWrngDznIAACEIqrlQ0A328.png "title=" 1.png " alt= "Wkiol1hwwrngdzniaaceiqrlq0a328.png"/>


As can be seen from the above figure, 10:36 log499.sh did not complete, 10 point 37 began to execute a new log499.sh script. This scripting conflict is certainly not what we want, so how can we ensure that the background only executes one script over a period of time?


This time we are going to use file lock, flock, this method is more than the judgment of the PID tall.


First of all, suppose our script name is abc.sh, this script file execution time is more than 1 minutes, and we set a lock file, the location is placed in/tmp/abc.lock, this file can be empty, and then # crontab-e, Add a command like this:

* * * * * * flock-xn/tmp/abc.lock-c ' sh/path/abc.sh >>/log path 2>&1 '

This time waits for crontab to start abc.sh, through # PS-EF|GREP ABC, to discover that there is always only one ABC process in the background.


But sometimes there is a problem, that is, after the execution of ABC, the next time the execution is not executed, as if Crontab failed, for such cases, you need to add the following statement to the end of abc.sh:

Rm-rf/tmp/abc.lock #删除掉原有的锁文件sleep n #睡n秒touch/tmp/abc.lock #再新建一个锁文件

This keeps updating the lock lock file, which guarantees that the crontab will be executed on schedule every time.


Notice here that I added a sleep n, where n is for the existence of a minute, in order to prevent a new lock lock file to be generated without going to the next minute, which would still be the case for a duplicate startup script.


Here is a principle of flock: each time the task is to try to get the lock file, if the lock file is taken, then the next step, and then discard the execution. A task is running when the lock file has been occupied, then the B task came, found no lock, will not carry out the task.


Here we use the three parameters of the flock:

-X,--exclusive: Get an exclusive lock

-N,--nonblock: If the lock is not immediately acquired, it fails directly instead of waiting

-C,--command: Run a separate command in the shell


Of course, flock still have a lot of rich parameters for you to use, we go to Baidu a bit.


Reference: http://blog.csdn.net/fdipzone/article/details/38284009

This article is from "Life is waiting for Gordo" blog, please make sure to keep this source http://chenx1242.blog.51cto.com/10430133/1889950

Crontab Troubleshooting Scripting Conflicts

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.