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.)
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
These files are stored at /var/spool/cron or /var/spool/cron/crontabs
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
Cron also offers some special strings, which can be used in place of the five time-and-date fields:
@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