Author |
Topic: crontab -- schedule tasks on Linux |
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
crontab -- schedule tasks on Linux |
If you want some maintenance tasks (creating backups, synchronizing caches, and scheduling update on statistics) to be automatically run in the background at regular intervals, you can achieve it on Linux by crontab.
The crontab (cron derives from chronos, Greek for time; tab stands for table) command, found in Unix and Unix-like operating systems, is used to schedule commands to be executed periodically. To see what crontabs are currently running on your system, you can open a terminal and run:
To edit the list of cronjobs you can run:
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
Crontab format |
Cronjobs are expressed in two formats: in number or in keyword.
Numberical format
* * * * * /bin/execute/this_script.sh
As you can see there are 5 stars. The stars represent different date parts in the following order: 1st * -- minute (from 0 to 59) 2nd * -- hour (from 0 to 23) 3rd * -- day of month (from 1 to 31) 4th * -- month (from 1 to 12) 5th * -- day of week (from 0 to 6) (0=Sunday)
Note: If you leave the star, or asterisk, it means every. By default, ***** means that the task is being executed every minute.
Keyword format
@<keyword> /bin/execute/this_script.sh
Here are some useful keywords instead of a number:
@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 * * * *
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
Examples |
Example #1. Run task at 1AM every Monday
0 1 * * 5 /bin/execute/this_script.sh
Example #2. Run task at 1AM every Monday to Friday
0 1 * * 1-5 /bin/execute/this_script.sh
Example #3. Run task daily
@daily /bin/execute/this_script.sh
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
Where is my crontab file actually stored? |
Check here:
/var/spool/cron/crontabs/
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
What's file /etc/crontab then? What's difference between /etc/crontab and /var/spool/cron/crontabs ? |
There are both crontab files with different format. They are both loaded when system is on but run in different environments:
/var/spool/cron/crontabs/ --> user defined and run in user's environment
/etc/crontab --> system's environment
Here is the default content of /etc/crontab
$ sudo cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
36 * * * * root run-parts /etc/cron.hourly
50 1 * * * root run-parts /etc/cron.daily
0 4 * * 0 root run-parts /etc/cron.weekly
30 3 14 * * root run-parts /etc/cron.monthly
|
|
|
|
|
|
|
Linux member offline |
|
posts: |
120 |
joined: |
01/24/2011 |
from: |
San Jose, CA |
|
|
|
|
|
/etc/cron.d vs. /etc/cron.daily |
What's difference between those two directories /etc/cron.d and /etc/cron.daily ?
As shown above in system crontab file /etc/crontab, all scripts within directory /etc/cron.daily will be executed daily (50 1 * * *) by system root's environment . For example, the script logrotate which rotates log file is a very good example to be here, as it needs to be run daily to ensure the disk capacity.
$ cat /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
/etc/cron.d, however, contains scripts of package level. In addition to read /etc/crontab, cron reads as well the files in /etc/cron.d: it treats the files in /etc/cron.d as in the same way as the /etc/crontab file (they follow the special format of that file, i.e. they include the user field). However, they are independent of /etc/crontab: they do not, for example, inherit environment variable settings from it. The intended purpose of this feature is to allow packages that require finer control of their scheduling than the /etc/cron.{daily,weekly,monthly} directories to add a crontab file to /etc/cron.d.
So, two key points: 1) Varies in control; 2) Varies in environment settings.
For example, the script awstats which update web access statistics based on access_log is a very good example to be here, as it needs to be run on package level (per vHost) and on finer control (you may want to run it at different time frame for each vHost).
List of scripts:
$ ls -l /etc/cron.d
-rw-r--r-- 1 root root 47 Jul 7 2008 awstatcronsetup
-rw-r--r-- 1 root root 151 Sep 6 23:55 awstats
AWStats cron setup:
$ cat /etc/cron.d/awstatcronsetup
55 23 * * * root /usr/share/awstats/cronsetup
AWStats script setup:
$ cat /usr/share/awstats/cronsetup
#!/bin/bash
hour=`echo $[$RANDOM % 24]`
if [ $hour -gt 9 ]; then
echo "00 $hour * * * root /usr/share/awstats/awstats_updateall.pl now
-awstatsprog=/var/www/cgi-bin/awstats/awstats.pl
-configdir=/etc/awstats/ >/dev/null 2>&1" >/etc/cron.d/awstats
else
echo "00 0$hour * * * root /usr/share/awstats/awstats_updateall.pl now
-awstatsprog=/var/www/cgi-bin/awstats/awstats.pl
-configdir=/etc/awstats/ >/dev/null 2>&1" >/etc/cron.d/awstats
fi
/sbin/service crond restart >/dev/null 2>&1
AWStats update cron script:
$ cat /etc/cron.d/awstats
00 18 * * * root /usr/share/awstats/awstats_updateall.pl now
-awstatsprog=/var/www/cgi-bin/awstats/awstats.pl
-configdir=/etc/awstats/ >/dev/null 2>&1
|
|
|
|
|
|
|
|