Crontab: Difference between revisions
No edit summary |
No edit summary |
||
(13 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
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. | 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. | ||
<code>crontab -e</code> Lets you edit cron jobs for the current user (You can use su to switch users to manage their cron jobs.) | <code>crontab -e</code> Lets you edit cron jobs for the current user (You can use su to switch users to manage their cron jobs.)<br /> | ||
== 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:<br /> | 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:<br /> | ||
<br /> | <br /> | ||
minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday). | minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday). | ||
< | |||
<br /> | <pre> | ||
# .---------------- 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 | |||
</pre> | |||
Note: <br/> | |||
These files are stored at /var/spool/cron or /var/spool/cron/crontabs | |||
== Examples == | |||
<code>01 04 1 1 1 /usr/bin/somedirectory/somecommand</code> | <code>01 04 1 1 1 /usr/bin/somedirectory/somecommand</code> | ||
Line 23: | Line 39: | ||
<code>*/10 * * * * /usr/bin/somedirectory/somecommand</code> | <code>*/10 * * * * /usr/bin/somedirectory/somecommand</code> | ||
== Special Strings == | |||
Cron also offers some special strings, which can be used in place of the five time-and-date fields: | Cron also offers some special strings, which can be used in place of the five time-and-date fields: | ||
String Meaning | '''String''' '''Meaning''' <br /> | ||
<br /> | |||
@reboot Run once, at startup.<br /> | @reboot Run once, at startup.<br /> | ||
@yearly Run once a year, "0 0 1 1 *".<br /> | @yearly Run once a year, "0 0 1 1 *".<br /> | ||
@annually (same as @yearly)<br /> | @annually (same as @yearly)<br /> | ||
@monthly Run once a month, "0 0 1 * *". <br /> | @monthly Run once a month, "0 0 1 * *". <br /> | ||
@weekly Run once a week, "0 0 * * 0".<br /> | |||
@weekly | @daily Run once a day, "0 0 * * *". <br /> | ||
@midnight (same as @daily)<br /> | |||
Run once a week, "0 0 * * 0". | @hourly Run once an hour, "0 * * * *". <br /> | ||
@daily | |||
Run once a day, "0 0 * * *". | == Check what cron jobs are setup == | ||
@midnight | |||
<code>crontab -l</code> - list the cron jobs for the current user | |||
(same as @daily) | |||
@hourly | <code>crontab -e</code> - edit the cron jobs for the current user | ||
Run once an hour, "0 * * * *". | Check cron jobs for all users (the following must be ran as root): <br /> | ||
<code>for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; done</code> | |||
== Run a php page as a cron job == | |||
Add ONE of the following lines: | |||
<code>45 * * * * /usr/bin/lynx -source http://example.com/cron.php</code> | |||
<code>45 * * * * /usr/bin/wget -O - -q -t 1 http://www.example.com/cron.php</code> | |||
<code>45 * * * * curl -s http://example.com/cron.php</code> | |||
<code>45 * * * * /usr/bin/php -q /home/$user/public_html/script.php >/dev/null 2>&1</code> | |||
== References == | == References == | ||
<ol> | <ol> | ||
<li></li> | <li>[http://man.he.net/?topic=crontab§ion=all Crontab Man Page]</li> | ||
<li>[https://help.ubuntu.com/community/CronHowto Cron How To]</li> | <li>[https://help.ubuntu.com/community/CronHowto Cron How To]</li> | ||
<li></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> | |||
<li>[http://www.htmlbasix.com/crontab.shtml Cron Job Creator]</li> | |||
</ol> | </ol> |
Latest revision as of 21:46, 20 August 2018
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).
# .---------------- 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
Note:
These files are stored at /var/spool/cron or /var/spool/cron/crontabs
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 * * * *".
Check what cron jobs are setup
crontab -l
- list the cron jobs for the current user
crontab -e
- edit the cron jobs for the current user
Check cron jobs for all users (the following must be ran as root):
for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; done
Run a php page as a cron job
Add ONE of the following lines:
45 * * * * /usr/bin/lynx -source http://example.com/cron.php
45 * * * * /usr/bin/wget -O - -q -t 1 http://www.example.com/cron.php
45 * * * * curl -s http://example.com/cron.php
45 * * * * /usr/bin/php -q /home/$user/public_html/script.php >/dev/null 2>&1