Information Technology Grimoire

Version .0.0.1

IT Notes from various projects because I forget, and hopefully they help you too.

Crontab Scheduling

Crontab has a robust scheduling system. There are 5 fields and you put a number in each to set the appropriate counter when your command or script triggers. The following examples will help you.

Scheduling in Crontab

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
# |  |  |  |  |
# *  *  *  *  *   command to be executed
  • fields are separated by spaces or tabs

  • commas can be used to specify a list

  • slashes can be used to step through every (ranges)

  • asteriks signify “all” for that field

Be careful of changing timezone. Your crontab could be affected each time you change them.

Crontab Schedule Examples

# hourly and don't mail it, send info to syslog
@hourly /path/to/script.sh >/dev/null 2>&1

# Create output log and watch for "ERROR" also create syslog
script.sh 2>&1 | tee -a output.log | grep -C 100 ERROR

# if as root, noon every day update
00 12 * * * apt-get update

# immediately after reboot (several issues though, recommended not to use)
@reboot /path/to/script.sh

# on reboot, mail uptime a
@reboot echo `uptime` | mail -s "`uname -n` rebooted" admin@site.com

# 60 seconds after reboot
@reboot sleep 60 && /path/to/script.sh

# every minute
*/1 * * * * /path/to/script.sh

# every hour on the 1st minute (1:01, 2:01, etc)
01 * * * * /path/to/script.sh

# every monday at midnight
0 0 * * MON /path/to/script.sh

# every midnight
0 0 * * * /path/to/script.sh

# every day at 9:15am
15 9 * * * /path/to/script.sh

# every other hour
0 */2 * * * /path/to/script.sh

You can type in and get human readable cron schedules by using this website: https://crontab.guru

Last updated on 20 Jul 2018
Published on 20 Jul 2018