Crontab cheat sheet: Difference between revisions

From Coolscript
Jump to navigation Jump to search
No edit summary
 
(5 intermediate revisions by the same user not shown)
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 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
{| style="text-align:left; background:#eeeeee;" border="1"
|-style="background:#aaaaaa"
! Weekday !! 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"
 
 
=== 2nd Thursday of month ===


* 2nd Thursday of  month
  # Example of job definition:
  # Example of job definition:
  # .---------------- minute (0 - 59)
  # .---------------- minute (0 - 59)
Line 18: 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 14 ] && 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


  0-7
===3rd Wednesday of month===
  8-15
# Example of job definition:
  16-23
# .---------------- minute (0 - 59)
  24-31
# | .------------- 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:
  # Example of job definition:
  # .---------------- minute (0 - 59)
  # .---------------- minute (0 - 59)
Line 35: Line 129:
  # |  |  .---------- 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 22 ] && [ $(date +\%d) -le 28 ] && /usr/bin/logger "Test 3rd Wed of month 03:05"
   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

Latest revision as of 08:09, 25 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 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

Weekday 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