Cron is one of the most useful elements of any *nix based system. Giving you an easy interface to run any command on a periodic basis with a down to the minute granularity. As a systems administrator or systems user you will find yourself using cron to schedule tasks on a regular basis. But with as useful as cron is and as frequently as it gets used, I regularly need a reference for the cron time string format. Hopefully this simple reference will help you and me remember the format for your future scheduled processes.
The Cron Time String Format
The cron time string has a simple format. Minute / Hour / Day of the Month / Month / Day of the week
|1||Minute||0 – 59, or * (Every Minute)|
|2||Hour||0 – 23, or * (Every Hour)|
|3||Day of the Month||1 – 31, or * (Every Day of the Month)|
|4||Month||1 – 12, , jan – dec, JAN – DEC or * (Every Month)|
|5||Day of the Week||0 – 7, sun – sat, SUN – SAT, (0 and 7 both equal Sunday), or * (Every Day of the Week)|
Cron Scheduling Examples
So given the values above a service that you would like to have run at 1:35 every day would be formatted like this.
35 1 * * *
But maybe we just want that process to run once a week we can modify the string and just add a value for the day of the week you want it to run. So for a process that you want to run every Thursday at 11:50PM you would format it like so.
50 23 * * 4
But you can then use modifiers to match some pretty specific time intervals for scheduling your process. The accepted modifiers are ‘/’ (skip a given number of values */3 for every 3rd, */10 for every 10th), ‘,’ (for a list of acceptable values), and ‘-‘ (for a range of values). So if you wanted to run a process at noon on the first day of every 3rd month you would write your cron time string like this.
0 12 1 */3 *
Or another example for a process that you want to run every 15 minutes from 2-5AM every Monday, Wednesday and Friday you would format your cron string like this.
*/15 2-5 * * 1,3,5
So now you know how to format your cron time string so that you can easily set your process to happen whenever you need it to run.
For more details on this topic visit this post on Formatting your Cron String or for help setting up your cron time string you can use the Crontab Guru’s interactive interface to create your time string.
And if you like this post check out one of our other posts on how to fix an APT NO_PUBKEY Error.