What is Cron?
Cron is a time-based job scheduler that comes default on most Unix like operatings systems and servers. It is designed to run command on a defined schedule. The crontab syntax is commonly used outside of the cron daemon (such as in Kuberenetes CronJobs) to define recuring schedules.
What does cron stand for?
The name cron most likely comes from the Greek word for time, χρόνος (chronos).
What is the longest interval between cron jobs
The longest possible time between two cron jobs being run is eight years, running on each leap day 0 0 29 2 *
. While leap days normally happen every four years, they do not occur in centennial years (e.g. 1800, 1900, etc...) that are not evenly divisible by 400.
How do I run a command every 5 minutes?
In your crontab add */5 * * * *
.
How do I run a command every 2 weeks?
The crontab syntax is pretty flexible but there is no one schedule that will run every two weeks. The trick is to run your command every week and to add a condition so the command is executed only on the correct weeks.
0 0 * * Sun
[ $(expr $(date +\%W) \% 2) -eq 1 ] && /path/to/command
\%
- Cron converts
%
to a newline character so we need to escape it (\
) in the command to get the right output date +\%W
- Retrieves the current week number
expr $(…) \% 2)
- Calculates the remainder of dividing the week number by 2. So
43 % 2 => 1
[ $(…) -eq 1 ] && /cmd
- Tests if it's an odd week (
-eq 1
) and only executes the command when it's true. Use-eq 0
to run on even weeks
How do I monitor cron jobs?
While the cron daemon is pretty battle tested and hardened, individual jobs can fail for a number of reason. For example, the path may not configured correctly, the schedule is wrong, or maybe the job ran but raised an exception.
For all of these cases we recommend Dead Man's Snitch to know when your cron jobs are failing to run or erroring.