Cron: Difference between revisions

From wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 22: Line 22:


* * means each tick is matched.
* * means each tick is matched.
* */5 means each 5th tick is matched
* */5 means each 5th tick is matched (0,5,10,...)
* A range is defined by a dash. E.g.  2-5 in minute  -> execute command on minute 2,3,4 and 5
* A range is defined by a dash. E.g.  2-5 in minute  -> execute command on minute 2,3,4 and 5
* A list is defined by comma's  E.g.  2,4,6 in minute -> execute command on minute 2, 4 and 6
* A list is defined by comma's  E.g.  2,4,6 in minute -> execute command on minute 2, 4 and 6
* For month and day-of-week also names can be used by giving the first 3 characters (case insensitive)
* For month and day-of-week also names can be used by giving the first 3 characters (case insensitive)
* For cron the week starts on Sunday so 0 is Sunday, 7 is Saturday
* In day-of-week numbers 0 and 7 are Sunday  


Example:
Example:
Line 34: Line 34:
00 12 * * 0 /usr/local/bin/myscript2.sh
00 12 * * 0 /usr/local/bin/myscript2.sh
</syntaxhighlight>
</syntaxhighlight>
Who can use cron can be limited in /etc/cron.[allow|deny]
* if cron.allow exists - only users listed into it can use crontab
* If cron.allow does not exist - all users except the users listed into cron.deny can use crontab
* If neither of the file exists - only the root can use crontab
* If a user is listed in both cron.allow and cron.deny - that user can use crontab.


;crontab -l
;crontab -l
Line 39: Line 45:


;crontab <crontabfile>
;crontab <crontabfile>
:Load the file into the crontab, all existing content will be replaced. In the end a user crontab is stored in /var/spool/cron/crontabs/<username>. Don't edit it there, the cron daemon will only read it if told so by the crontab command.
:Load the file into the crontab, all existing content will be replaced. A user's crontab is stored in /var/spool/cron/crontabs/<username>. Don't edit it there, the cron daemon will only read it if told so by the crontab command.


;crontab -e
;crontab -e
:Edit the current crontab directly in the default editor. Change the default editor by setting the EDITOR environment variable.
:Edit the current crontab directly in the default editor. Change the default editor by setting the EDITOR environment variable.

Latest revision as of 13:41, 1 January 2026

Timed execution of commands. The cron daemon is checking the crontab every minute to see if anything needs to be done

Format of crontab
minute hour day-of-month month day-of-week command
1-59 1-23 1-31 1-12 0-7 doit

In scheduling fields (all except the command) following applies:

  • * means each tick is matched.
  • */5 means each 5th tick is matched (0,5,10,...)
  • A range is defined by a dash. E.g. 2-5 in minute -> execute command on minute 2,3,4 and 5
  • A list is defined by comma's E.g. 2,4,6 in minute -> execute command on minute 2, 4 and 6
  • For month and day-of-week also names can be used by giving the first 3 characters (case insensitive)
  • In day-of-week numbers 0 and 7 are Sunday

Example: Every Sunday at noon execute my script:

00 12 * * sun /usr/local/bin/myscript.sh
00 12 * * 0 /usr/local/bin/myscript2.sh

Who can use cron can be limited in /etc/cron.[allow|deny]

  • if cron.allow exists - only users listed into it can use crontab
  • If cron.allow does not exist - all users except the users listed into cron.deny can use crontab
  • If neither of the file exists - only the root can use crontab
  • If a user is listed in both cron.allow and cron.deny - that user can use crontab.
crontab -l
List the current crontab
crontab <crontabfile>
Load the file into the crontab, all existing content will be replaced. A user's crontab is stored in /var/spool/cron/crontabs/<username>. Don't edit it there, the cron daemon will only read it if told so by the crontab command.
crontab -e
Edit the current crontab directly in the default editor. Change the default editor by setting the EDITOR environment variable.