Crontab cheat sheet: Difference between revisions

From Coolscript
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:


* 1st Thursday of month
=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 Afternoon"
 
=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
{| style="text-align:left; background:#eeeeee;" border="1"
|-style="background:#aaaaaa"
! Week !! Possible 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:
  # Example of job definition:
Line 7: Line 92:
  # |  |  .---------- day of month (1 - 31)
  # |  |  .---------- day of month (1 - 31)
  # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
  # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
  # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
  # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR mon, tue, wed ...
  # |  |  |  |  |
  # |  |  |  |  |
  # *  *  *  *  *  command to be executed
  # *  *  *  *  *  command to be executed
   0  15 *  *  4   [ $(date +\%d) -le 07 ] && command
   0  15 *  *  mon   [ $(date +\%d) -le 07 ] && /usr/bin/logger "1st Mon of month 15:00"




0-7    1st week
=== 2nd Thursday of month ===
7-14  2nd week
14-21  3rd week experimental
21-28  4th week experimental
28-31  5th week experimental


* 2nd Thursday of  month
  # Example of job definition:
  # Example of job definition:
  # .---------------- minute (0 - 59)
  # .---------------- minute (0 - 59)
Line 25: Line 105:
  # |  |  .---------- day of month (1 - 31)
  # |  |  .---------- day of month (1 - 31)
  # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
  # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
  # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
  # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR mon, tue, wed ...
  # |  |  |  |  |
  # |  |  |  |  |
  # *  *  *  *  *  command to be executed
  # *  *  *  *  *  command to be executed
   0  15 *  *  [ $(date +\%d) -ge 8 ] && [ $(date +\%d) -le 15 ] && command
   0  15 *  *  thu  [ $(date +\%d) -ge 8 ] && [ $(date +\%d) -le 14 ] && /usr/bin/logger "2nd Thursday of month 15:00"




*3rd wednesday of month
 
===3rd Wednesday of month===
  # Example of job definition:
  # Example of job definition:
  # .---------------- minute (0 - 59)
  # .---------------- minute (0 - 59)
Line 37: Line 118:
  # |  |  .---------- day of month (1 - 31)
  # |  |  .---------- day of month (1 - 31)
  # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
  # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
  # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
  # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR mon, tue, wed ...
  # |  |  |  |  |
  # |  |  |  |  |
  # *  *  *  *  *  command to be executed
  # *  *  *  *  *  command to be executed
   5  03 *  *  WED  [ $(date +\%d) -ge 21 ] && [ $(date +\%d) -le 28 ] && /usr/bin/logger "Test 3rd Wed of month 03:05"
   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
=Reference=
https://crontab.guru
<br>
https://serverfault.com/questions/986514/crontab-first-wednesday-of-the-month-that-is-followed-by-the-first-monday-of-t
https://serverfault.com/questions/986514/crontab-first-wednesday-of-the-month-that-is-followed-by-the-first-monday-of-t
<br>
<br>
https://stackoverflow.com/questions/58413332/how-to-schedule-a-cron-for-the-first-thursday-of-every-month
https://stackoverflow.com/questions/58413332/how-to-schedule-a-cron-for-the-first-thursday-of-every-month

Revision as of 21:53, 24 January 2023

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 Afternoon"

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 Possible 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