Crontab cheat sheet

From Coolscript
Jump to navigation Jump to search

Range Values

Afternoon

# 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) OR mon, tue, wed ...
# |  |     |  |  |
# *  *     *  *  *   command to be executed
  *  12-23 *  *  *  /usr/bin/logger "Crontab Afternoon"


Weekdays

# 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) OR mon, tue, wed ...
# |  |  |  |   |
# *  *  *  *   *       command to be executed
  *  *  *  *  1-5      /usr/bin/logger "Crontab Weekdays"

Winter

# 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) OR mon, tue, wed ...
# |  |  |  |     |
# *  *  *  *     *   command to be executed
  *  *  *  12-03 *   /usr/bin/logger "Crontab Winter"

Step Values

Every Minute

# 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) OR mon, tue, wed ...
# |   |  |  |  |
# *   *  *  *  *   command to be executed
  */1 * *  *  *  /usr/bin/logger "Crontab Every minute"

Every Hour

# 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) OR mon, tue, wed ...
# |  |   |  |  |
# *  *   *  *  *   command to be executed
  0  */1 *  *  *  /usr/bin/logger "Crontab Every hour"

Fixed Values

@yearly  /usr/bin/logger "Crontab Every Year"	 
@monthly /usr/bin/logger "Crontab Every Month"	  
@weekly  /usr/bin/logger "Crontab Every Week"	  	
@daily	 /usr/bin/logger "Crontab Every Day"	  	
@hourly  /usr/bin/logger "Crontab Every Hour"	  	
@reboot  /usr/bin/logger "Crontab On Reboot"

Run every weekday of month

Possible examples

Week Day range Condition
1st 1-7 [ $(date +\%d) -le 07 ]
2nd 8-14 [ $(date +\%d) -ge 8 ] && [ $(date +\%d) -le 14 ]
3rd 15-21 [ $(date +\%d) -ge 15 ] && [ $(date +\%d) -le 21 ]
4th 22-28 [ $(date +\%d) -ge 22 ] && [ $(date +\%d) -le 28 ]
5th 29-31 [ $(date +\%d) -ge 29 ]

Examples

1st Monday of month

# 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) OR mon, tue, wed ...
# |  |  |  |  |
# *  *  *  *  *   command to be executed
  0  15 *  *  mon   [ $(date +\%d) -le 07 ] && /usr/bin/logger "1st Mon of month 15:00"


2nd Thursday of month

# 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) OR mon, tue, wed ...
# |  |  |  |  |
# *  *  *  *  *   command to be executed
  0  15 *  *  thu  [ $(date +\%d) -ge 8 ] && [ $(date +\%d) -le 14 ] && /usr/bin/logger "2nd Thursday of month 15:00"


3rd Wednesday of month

# 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) OR mon, tue, wed ...
# |  |  |  |  |
# *  *  *  *  *   command to be executed
  5  03 *  *  WED   [ $(date +\%d) -ge 15 ] && [ $(date +\%d) -le 21 ] && /usr/bin/logger "Test 3rd Wed of month 03:05"

4th Friday of month

# 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) OR mon, tue, wed ...
# |  |  |  |  |
# *  *  *  *  *   command to be executed
  5  03 *  *  fri   [ $(date +\%d) -ge 22 ] && [ $(date +\%d) -le 28 ] && /usr/bin/logger "Test 4th Fri of month 03:05"

5th Sunday of month

  • Note: This event might not always occur, eg February vs leap year
# 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) OR mon, tue, wed ...
# |  |  |  |  |
# *  *  *  *  *   command to be executed
  5  03 *  *  7 [ $(date +\%d) -ge 29 ]  && /usr/bin/logger "Test 5th Sub of month 03:05"

Reference

https://crontab.guru
https://serverfault.com/questions/986514/crontab-first-wednesday-of-the-month-that-is-followed-by-the-first-monday-of-t
https://stackoverflow.com/questions/58413332/how-to-schedule-a-cron-for-the-first-thursday-of-every-month