Difference between revisions of "Cron"

From wiki
Jump to navigation Jump to search
m
 
(2 intermediate revisions 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
 
* 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
  
 
Example:
 
Example:
Every Sunday at noon execute my script:<br>
+
Every Sunday at noon execute my script:
<code>00 12 * * sun /usr/local/bin/myscript.sh</code>
+
<syntaxhighlight lang=bash>
 +
00 12 * * sun /usr/local/bin/myscript.sh
 +
00 12 * * 0 /usr/local/bin/myscript2.sh
 +
</syntaxhighlight>
 +
 
 +
Who can user 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
 
:List the current crontab
 
:List the current crontab
 +
 +
;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.
 +
 +
;crontab -e
 +
:Edit the current crontab directly in the default editor. Change the default editor by setting the EDITOR environment variable.

Latest revision as of 10:16, 9 October 2023

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
  • 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)
  • For cron the week starts on Sunday so 0 is Sunday, 7 is Saturday

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 user 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. 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.
crontab -e
Edit the current crontab directly in the default editor. Change the default editor by setting the EDITOR environment variable.