Simple protection against dual cron job launch

    I want to talk about a simple script that allows you to protect yourself from double-running cron jobs.

    For example, once a minute you update a certain cache in order to quickly give it to millions of site visitors. Everything goes fine, but exactly until the weekly backup starts in the dead of night and your cache is formed not in 10 seconds, but in 70, and at the 60th second one more process of cache formation will overtake it.

    What will happen in such a scenario further is a very interesting question. It is very likely that the two processes will actively interfere with each other (they work with the same objects), and their total execution time will by no means be twice as long as usual, but if the third one catches up ...

    The described situation is banal to indecent, although a degenerate version is more common: there is no crown, just at midnight at the time the cache expires, each new visitor starts a thick process and the server turns into a pumpkin .
    As a developer, you must foresee such a scenario and protect yourself from it. But if Pinocchio was stupid, the developers of your software did not take care of this, you will have to save yourself.

    I use a simple and convenient lockrun utility. The principle of its operation is simple: for each process it creates a file and hangs a lock on it. As soon as the process is completed, the lock disappears. Lock also disappears in the event of a sudden death of the process, and there is no need to check the pid for existence or do other body movements. If the process is restarted, and the lock file has not yet been freed, the script is interrupted and a message is issued in STDERR.

    The utility is written in C, so before using it you will have to compile it on the target machine. So, download, compile and put where necessary: If you are not root, you will have to neglect the last line and either indicate the full path or change PATH. Usage example: Actually, the lockrun command and parameters are separated by two minuses. The following parameters are accepted:

    $ wget unixwiz.net/tools/lockrun.c
    $ gcc lockrun.c -o lockrun
    $ sudo cp lockrun /usr/local/bin/





    * * * * * /usr/local/bin/lockrun --lockfile=/tmp/megacache.lockrun -- /path/to/megacache/generator



    --lockfile = / path / to / file
    Required parameter specifying the file name for the lock. If there is no such file, it will be created automatically. Of course, for each job there should be a file.
    --maxtime = N The
    time in seconds allocated to the script for “normal” operation. If the script worked longer, a message will be displayed in STDERR, which cron can send to your mail.
    --wait
    If this parameter is specified, lockrun will not cancel the execution of the script, but will wait until the previous process releases the lock.
    --verbose
    As always, providing more detailed information on the progress of the process.
    --quiet
    Do not report error messages. You can enable it if refusing to start the task is not a serious problem.

    That's all. As you can see, it is really simple and effective.

    UPD: In the comments I was informed that there is also a native tool for Linux.
    Of course, no one claimed that the described utility is the only solution.

    Also popular now: