Cron: Difference between revisions

From Leo's Notes
This page was last edited on 9 May 2019, at 20:41.
m Text replacement - "{{Highlight" to "{{highlight"
No edit summary
Line 3: Line 3:
== Basic Usage ==
== Basic Usage ==
The basic usage is to create a crontab file containing the following fields:  
The basic usage is to create a crontab file containing the following fields:  
{{highlight|lang=text|code=
{{highlight|lang=terminal|code=
<nowiki>
# Example of job definition:
# Example of job definition:
# .---------------- minute (0 - 59)
# .---------------- minute (0 - 59)
Line 12: Line 13:
# |  |  |  |  |
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
# *  *  *  *  * user-name command to be executed
</nowiki>
}}
}}


Cronjobs can be added by modifing any of the files below:
{{Info|About the 'Day of Week' value|
If you set the 'Day of Week' value to something other than '*', the cronjob will run on either the specified day of month '''or''' the specified day of week.
 
For instance, this cronjob will run at 2:15AM between the 1st and 7th of every month AND at 2:15AM every Wednesdays. It does '''not''' run on the first Wednesdays of each month.
{{highlight|lang=terminal|code=
15 2 1-7 * 3 root run-every-wednesdays-and-1-7-every-month
}}
 
If you wish to run a cronjob on the first Wednesday of each month, you need to add a check with {{code|test $(date %u) -eq 3}}:
{{highlight|lang=terminal|code=
15 2 1-7 * 3 root test $(date +\%u) -eq 3 && run-on-first-wednesdays-every-month
}}
 
}}
 
Cronjobs can be added by modifying any of the files below:
{{highlight|lang=text|code=
{{highlight|lang=text|code=
/etc/crontab
/etc/crontab
Line 27: Line 44:
@daily = 0 0 * * *
@daily = 0 0 * * *
}}
}}
== Notes ==
Defining both a day of month range and day of week value will cause the job to run when any of the two conditions are met.


[[Category:Linux]]
[[Category:Linux]]

Revision as of 20:41, 9 May 2019

Cron is a program that runs scheduled tasks on Unix based systems. On Linux, the implementation is based on the new Vixie Cron code base.

Basic Usage

The basic usage is to create a crontab file containing the following fields:

# 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 sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
About the 'Day of Week' value

If you set the 'Day of Week' value to something other than '*', the cronjob will run on either the specified day of month or the specified day of week.

For instance, this cronjob will run at 2:15AM between the 1st and 7th of every month AND at 2:15AM every Wednesdays. It does not run on the first Wednesdays of each month.

15 2 1-7 * 3 root run-every-wednesdays-and-1-7-every-month

If you wish to run a cronjob on the first Wednesday of each month, you need to add a check with test $(date %u) -eq 3:

15 2 1-7 * 3 root test $(date +\%u) -eq 3 && run-on-first-wednesdays-every-month


Cronjobs can be added by modifying any of the files below:

/etc/crontab
/var/spool/cron/
/etc/crond.d/*
/etc/anacrontab

Common Times

Instead of defining the specific minute, hour, and date for a recurring task, you can use shorthand keywords:

@daily = 0 0 * * *