About once every 2 years, I get a new machine and have to set up crontab locally all over again… and completely forget how to do it. Wrote this up as a reminder for future me.

How to read a cron entry

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                                       7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * *  command to execute

Special Strings:

  • @reboot – run once when machine starts
  • @yearly or @annualy – run once a year
  • @monthly – run once a month
  • @weekly – run once a week
  • @daily or @midnight – run once a day
  • @hourly – run hourly

Examples

At 12:00 pm (noon) every day during the year 2017:

0 12 * * ? 2017

Every 5 minutes starting at 1 pm and ending on 1:55 pm and then starting at 6 pm and ending at 6:55 pm, every day:

0 0/5 13,18 * * ?

Every minute starting at 1 pm and ending on 1:05 pm, every day:

0-5 13 * * ?

At 1:15 pm and 1:45 pm every Tuesday in the month of June:

15,45 13 ? 6 Tue

At 9:30 am every Monday, Tuesday, Wednesday, Thursday, and Friday:

30 9 ? * MON-FRI

At 9:30 am on 15th day of every month:

30 9 15 * ?

At 6 pm on the last day of every month:

0 18 L * ?

At 6 pm on the 3rd to last day of every month:

0 18 L-3 * ?

At 10:30 am on the last Thursday of every month:

30 10 ? * 5L

Testing cron locally:

On a brand new Unix machine, a simple way to check that cron is running locally is as follows.

  1. Create a shell script that simply writes the current timestamp to a file
$ vi ~/dev/test.sh

Contents of test.sh:

date >> /home/vishal/dev/testOutput.txt

Remember to create the testOutput.txt file beforehand - and modify the permissions of both the test.sh and testOutput.txt files to be executable.

$ chmod +x test.sh
$ chmod +x testOutput.txt
  1. Edit your crontab

Now we’ll use the above script to test our cron.

Edit your crontab:

$ crontab -e

The file may be empty, or look like this:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

add a new cron line at the bottom:

* * * * * /home/vishal/dev/test.sh 1>> /dev/null 2>> /home/vishal/dev/errors.log

This will run our test.sh script once a minute; piping any output to /dev/null and any errors to an errors.log file

Now save your crontab, and tail your test output file:

cron 🍺  tail -f testOutput.txt
Tue Apr 24 14:38:00 CDT 2018
Tue Apr 24 14:39:01 CDT 2018
Tue Apr 24 14:40:00 CDT 2018
Tue Apr 24 14:41:00 CDT 2018
Tue Apr 24 14:42:00 CDT 2018
Tue Apr 24 14:43:00 CDT 2018
Tue Apr 24 14:44:00 CDT 2018
Tue Apr 24 14:45:00 CDT 2018
Tue Apr 24 14:46:00 CDT 2018
Tue Apr 24 14:47:00 CDT 2018
Tue Apr 24 14:48:00 CDT 2018
Tue Apr 24 14:49:00 CDT 2018
Tue Apr 24 14:50:00 CDT 2018
Tue Apr 24 14:51:01 CDT 2018
Tue Apr 24 14:52:00 CDT 2018
Tue Apr 24 14:53:00 CDT 2018
Tue Apr 24 14:54:00 CDT 2018
Tue Apr 24 14:55:00 CDT 2018

Voila! Your crontab is working!

For more help

Intro to cron

crontab.guru

cron expression examples