2020-09-05
|~2 min read
|266 words
Cron jobs are awesome, but I often forget the syntax. In the process of learning to write one of my first cron jobs, I found this diagram in a blog post about scheduling Netlify Deploys with Github Actions quite useful and reference it frequently:
┏━━━━━━━━━━━━ minute (0 - 59)
┃ ┏━━━━━━━━━━ hour (0 - 23)
┃ ┃ ┏━━━━━━━━ day of month (1 - 31)
┃ ┃ ┃ ┏━━━━━━ month (1-12)
┃ ┃ ┃ ┃ ┏━━━━ day of week (0 - 6) or use names;
┃ ┃ ┃ ┃ ┃ 0 and 7 are Sunday, 1 is Monday,
┃ ┃ ┃ ┃ ┃ 2 is Tuesday, etc.
┃ ┃ ┃ ┃ ┃
* * * * * <command to execute>
Even still, there are some things that it glosses over.
Like how to run something every 15 minutes:
*/15 * * * *
Or how to run a cron job twice a day at 8am and 6pm:
* 8,18 * * *
Or how about every month from the 1st through the 5th:
* * 1-5 * *
Each section of the cron syntax has different accepted values, but they all share the four operators demonstrated above:
* any value
, value list separator
- range of values
/ step values
Crontab.guru’s simple tool for validating a cron job syntax is awesome and I learned a lot just by clicking the “random” button and seeing what came up. If nothing else, I now understand that the syntax is much more versatile than was immediately obvious to me.
Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!