cron
In Linux, tasks can be configured to run automatically during a specified time period, on a specified date, or when the average system load falls below a specified amount. Red Hat Enterprise Linux is preconfigured to run critical system tasks so that the system can be kept updated. For example, the slocate database used by the locate command is updated daily. System administrators can use automated tasks to perform scheduled backups, monitor systems, run custom scripts, and more.
Red Hat Enterprise Linux comes with several tools to automate tasks: cron, at, and batch.
Cron is a daemon process that can be used to schedule the execution of recurring tasks based on a combination of time, date, month, and week.
cron assumes the system is running continuously. If the system is not running when a task is scheduled, the task will not be executed.
To use the cron service, you must have the vixie-cron RPM package installed and the crond service must be running. To determine whether the package is installed, use the rpm -q vixie-cron command. To determine whether the service is running, use the /sbin/service crond status command.
Configure cron tasks
The main configuration file of cron is /etc/crontab, which includes the following lines:
<tt class="COMPUTEROUTPUT">SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # run-parts 01 * * * * root run-parts /etc/cron.hourly 02 4 * * * root run-parts /etc/cron.daily 22 4 * * 0 root run-parts /etc/cron.weekly 42 4 1 * * root run-parts /etc/cron.monthly</tt> |
The first four lines are variables used to configure the cron task running environment. SHELL The value of the variable tells the system which shell environment to use (in this case, bash shell); PATH The variable definition is used The path to execute the command. The output of the cron job is mailed to the username defined by the MAILTO variable. If the MAILTO variable is defined as a blank string (MAILTO=""), the email will not be sent. The HOME variable can be used to set the home directory used when executing commands or scripts.
/etc/crontab Each line in the file represents a task, and its format is:
<tt class="COMPUTEROUTPUT">minute hour day month dayofweek command</tt> |
minute — 分钟,从 0 到 59 之间的任何整数
hour — 小时,从 0 到 23 之间的任何整数
day — 日期,从 1 到 31 之间的任何整数(如果指定了月份,必须是该月份的有效日期)
month — 月份,从 1 到 12 之间的任何整数(或使用月份的英文简写如 jan、feb 等等)
dayofweek — 星期,从 0 到 7 之间的任何整数,这里的 0 或 7 代表星期日(或使用星期的英文简写如 sun、mon 等等)
command — 要执行的命令(命令可以是 ls /proc >> /tmp/proc 之类的命令,也可以是执行你自行编写的脚本的命令。)
在以上任何值中,星号(*)可以用来代表所有有效的值。譬如,月份值中的星号意味着在满足其它制约条件后每月都执行该命令。
整数间的短线(-)指定一个整数范围。譬如,1-4 意味着整数 1、2、3、4。
用逗号(,)隔开的一系列值指定一个列表。譬如,3, 4, 6, 8 标明这四个指定的整数。
正斜线(/)可以用来指定间隔频率。在范围后加上 /integer> 意味着在范围内可以跳过 integer。譬如,0-59/2 可以用来在分钟字段定义每两分钟。间隔频率值还可以和星号一起使用。例如,*/3 的值可以用在月份字段中表示每三个月运行一次任务。
开头为井号(#)的行是注释,不会被处理。
如你在 /etc/crontab 文件中所见,它使用 run-parts 脚本来执行 /etc/cron.hourly、/etc/cron.daily、/etc/cron.weekly 和 /etc/cron.monthly 目录中的脚本,这些脚本被相应地每小时、每日、每周、或每月执行。这些目录中的文件应该是 shell 脚本。
如果某 cron 任务需要根据调度来执行,而不是每小时、每日、每周、或每月地执行,它可以被添加到 /etc/cron.d 目录中。该目录中的所有文件使用和 /etc/crontab 中一样的语法。
<tt class="COMPUTEROUTPUT"># record the memory usage of the system every monday # at 3:30AM in the file /tmp/meminfo 30 3 * * mon cat /proc/meminfo >> /tmp/meminfo # run custom script the first day of every month at 4:10AM 10 4 1 * * /root/scripts/backup.sh</tt> |
Example 37-1. crontab example
Users other than root can use the crontab tool to configure cron tasks. All user-defined crontabs are saved in the /var/spool/cron directory and executed using the identity of the user who created them. To create a crontab project as a user, log in as that user and type the crontab -e command using either the VISUAL or EDITOR The editor specified by the environment variable to edit the user's crontab. This file uses the same format as /etc/crontab. When the changes to the crontab are saved, the crontab file is saved according to the username and written to the file /var/spool/cron/username in.
Thecron daemon checks every minute the /etc/crontab file, the etc/cron.d/ directory, and the directory. 🎜>/var/spool/cron
Changes in the directory. If changes are found, they are loaded into memory. This way, you don't have to restart the daemon when a crontab file changes.The /etc/cron.allow and /etc/cron.deny files are used to restrict the use of cron. Both formats using control files are one user per line. No spaces are allowed in both files. The cron daemon (crond
) does not have to be restarted if the usage control file is modified. Use a control file that is read every time a user adds or removes a cron task.Root can always use cron regardless of what is specified in the usage control file.
If the cron.allow file exists, only users listed in it are allowed to use cron, and cron.deny
The file will be ignored.If the cron.allow file does not exist, all users listed in cron.deny
are banned from using cron .To start the cron service, use the /sbin/service crond start command. To stop the service, use the /sbin/service crond stop
command. It is recommended that you start this service at boot time.