Skip to content

Use crontab and flock to control cron job sequence

homepage-banner

Introduction

Cron is a time-based job scheduler in Unix-like operating systems. It is used to automate scheduled tasks and scripts. Cron jobs are executed at pre-defined times and intervals. However, sometimes the sequence of execution of these jobs is important. In this blog post, we will discuss how to use crontab and flock to control the sequence of cron jobs.

Using crontab

Crontab is a command that is used to create, edit, and manage cron jobs. By default, cron jobs are executed in random order. To control the sequence of execution, we can use the sleep command to introduce a delay between jobs. For example, if we want to execute job1, wait for 10 seconds, and then execute job2, we can add the following lines to the crontab file:

* * * * * /path/to/job1
* * * * * sleep 10; /path/to/job2

This will execute job1 every minute, followed by job2 with a 10-second delay.

Using flock

Flock is a command that is used to manage file locks. It can be used to prevent multiple instances of a script from running simultaneously. This can be useful when we have a cron job that takes a long time to execute and we want to ensure that it completes before the next job starts. To use flock, we need to modify the crontab entry for the job we want to lock. For example, if we want to lock the job1 script, we can modify the crontab entry as follows:

* * * * * /usr/bin/flock -n /tmp/job1.lockfile /path/to/job1

This will ensure that only one instance of the job1 script is running at any given time. If the lockfile already exists, flock will prevent the script from running until the lock is released.

Controlling job sequence using flock and crontab

We can also use flock and crontab together to control the sequence of cron jobs. For example, if we want to execute job1, wait for it to complete, and then execute job2, we can modify the crontab entries as follows:

* * * * * /usr/bin/flock -n /tmp/job1.lockfile /path/to/job1
* * * * * /usr/bin/flock -n /tmp/job2.lockfile /path/to/job2

This will ensure that job2 does not start until job1 completes. If job1 is already running, job2 will wait until the lock is released.

Conclusion

In this blog post, we have seen how to use crontab and flock to control the sequence of cron jobs. By introducing delays and file locks, we can ensure that our cron jobs are executed in the desired order. This can be useful when we have dependencies between jobs or when we want to prevent multiple instances of a script from running simultaneously.

Appendix

*/2 * * * * /usr/bin/flock -xn /var/run/cron.lock -c '/home/cron.sh'

Parameter Description

-s, --shared: Obtain a shared lock.
-x, --exclusive: Obtain an exclusive lock.
-u, --unlock: Remove a lock, usually unnecessary as the lock is automatically released after the script finishes executing.
-n, --nonblock: Fail immediately if the lock cannot be obtained instead of waiting.
-w, --timeout: Wait for the specified time if the lock cannot be obtained immediately.
-o, --close: Close the file descriptor before running the command. This is used if the command generates a child process that is not controlled by the lock.
-c, --command: Run a single command in the shell.
Leave your message