Crontab: Difference between revisions
No edit summary |
|||
Line 60: | Line 60: | ||
<li>[https://help.ubuntu.com/community/CronHowto Cron How To]</li> | <li>[https://help.ubuntu.com/community/CronHowto Cron How To]</li> | ||
<li>[https://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/ How to add Cron jobs]</li> | <li>[https://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/ How to add Cron jobs]</li> | ||
<li>[http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/ Geek Stuff 15 crontab examples]</li> | |||
</ol> | </ol> |
Revision as of 18:36, 19 January 2017
Cron is the name of program that lets you execute commands or scripts automatically at a specified time/date. Crontab is the program to mange cron jobs.
crontab -e
Lets you edit cron jobs for the current user (You can use su to switch users to manage their cron jobs.)
Fields
Each line has five time-and-date fields, followed by a command, followed by a newline character ('\n'). The fields are separated by spaces. The five time-and-date fields cannot contain spaces. The five time-and-date fields are as follows:
minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday).
* * * * * command to be executed - - - - - | | | | | | | | | ----- Day of week (0 - 7) (Sunday=0 or 7) | | | ------- Month (1 - 12) | | --------- Day of month (1 - 31) | ----------- Hour (0 - 23) ------------- Minute (0 - 59)
Examples
01 04 1 1 1 /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 4:01am on January 1st plus every Monday in January.
An asterisk (*) can be used so that every instance (every hour, every weekday, every month, etc.) of a time period is used
01 04 * * * /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 4:01am on every day of every month.
You may want to run a script some number of times per time unit. For example if you want to run it every 10 minutes use the following crontab entry (runs on minutes divisible by 10: 0, 10, 20, 30, etc.)
*/10 * * * * /usr/bin/somedirectory/somecommand
Special Strings
Cron also offers some special strings, which can be used in place of the five time-and-date fields:
String Meaning
@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".