Note: Recently, in Redhat 9.0, you have to use crontab to implement the authentication of the task, search this article, and it is still very detailed. Original text at http://www.hk.redhat.com/docs/manuals/linux/rhl-9-manual-tw/custom-guide/index.html
Chapter 28 Automatic Work Scheduling Management
In Linux, you can set the system's work in a specified time, date, or when the system average load is lower than the specified data. Red Hat Linux has previously set important system work to keep the system update, such as the Slocate database used by the Locate instruction is updated daily. System administrators can use automatic work management to perform regular backups, monitor the system and execution of the program, and so on.
Red Hat Linux contains four automated work management programs: Cron, Anacron, AT, and BATCH.
28.1. Cron
Cron is a system program (which can schedule the execution of the repetitability based on the set time, date, month, week, and weeks.
Cron assumes that the system is in the case of continuous start, if the system is scheduled, the system does not boot, the work will not be executed. To set the work if you want to set up a period of time instead of the exact time, please refer to Section 28.2. To schedule only a job with only one job, please refer to Section 28.3.
To use cron's service, you must install the Vixie-Cron RPM kit, and crones must be in execution. To see if the kit is installed, use the rpm -q vixie-cron command; if you want to see if the service is in execution, use the / sbin / service crd Status command.
28.1.1. Set the work of cron
The main setting file / etc / crontab contains the following lines:
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.warekly
42 4 1 * * root run-parts /etc/cron.monthly
The first four lines are used to set the variables of the environment to be executed. For example, the value of the shell variable will tell the system to use the shell environment (here as Bash Shell), and the PATH variable defines the execution of the instruction path of. The output of CRON work will be sent to the user name defined by email to the mailto variable. If the MailTo variable is defined as an empty string (mailto = "), any email will not be transmitted. Home variables can be used to set the home directory to be used when performing instructions or program code.
Each line in the / etc / crontab file represents a work, and has the same format:
Time date month Weekly Directive
· Division - any integer from 0 to 59
• Time - any integer from 0 to 23
• Date - any integer from 1 to 31 (if there is a specified month, must be a valid date)
· Month - any integer from 1 to 12 (or abbreviation, such as Jan, Feb, etc.)
· Weekly - Any integer from 0 to 7, 0 or 7 represents Sunday (or abbreviated name, such as Sun, MON, etc.) · Instruction-to be executed (here the instructions here can be, for example, LS / Proc >> / TMP / PROC directive or instructions used to perform program code you write.)
For any value above, an asterisk (*) can be used to specify all valid values, such as an asterisk in the month represents the instruction per month in the range of other values.
One quotation number (-) between integers represents an integer, such as 1-4 represents integer 1, 2, 3 and 4.
A series of values separated by a comma (,) represents a list, such as 3, 4, 6, 8 represents these four specific integers.
The slash symbol (/) is used to represent the value of the time interval, as long as the / Any row starting with a well number (#) is an annotation and will not be executed. As you can see / etc / crontab file, it uses the Run-Parts program code to perform /tc/cron.Hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly directory In each hour, every day, every day or month-based program. The files in these catalogs should be the shell code. If a CRON work needs to be executed in the expected time, instead of every hour, every day, every day or monthly basis, you can add it to the /etc/cron.d directory, all files in this directory Use the same syntax as the / etc / crontab file, please refer to Example 28-1 to achieve the relevant examples. # Record the memory usage of the system every monday # at 3:30 am in the file / tmp / meminfo 30 3 * * MON CAT / Proc / Meminfo >> / TMP / MEMINFO # Run Custom Script The First Day of Every Month At 4:10 am 10 4 1 * * /Root/scripts/backup.sh Example 28-1. Example of crontab Users outside root can use the crontab tool to set the CRON's work, all user-defined crontabs are stored in the / var / spool / cron directory and is executed by the user name of the user who creates their users. To create a crontab with a user, log in and enter the crontab -e instruction to edit the user's crontab using the user to edit the user using the editor specified by the Visual or Editor environment variable. This file uses the same format as the / etc / crontab file. After the change made of CRONTAB, the crontab will be stored in the / var / spool / cron / username file in accordance with the user name and written to the / var / spool / cron / usrname file. The Cron system program will check the / etc / crontab file, / etc / cron.d / and / var / spool / cron directory, if there is any change, they will be loaded into the memory . So if you change this system after the CRONTAB file change is required. 28.1.2. CRON's access control The /etc/cron.Allow and /etc/cron.deny file is used to limit the user's access rights to cron. The format of these two access control files is to place a user name at each row, and there is no rooms in both files. If you modify the access control file, you do not need to restart the Cron System Code (crred), which will be read when the user tries to increase or delete a CRON work. Root users can use cron at any time, regardless of whether the user's name is listed in the access control file. If the cron.allow file exists, only the user listed in it allows the use of cron, and the cron.deny file is ignored. If the cron.allow file does not exist, all users listed in the Cron.deny file are not allowed to use cron. 28.1.3. Launch and stop service To start the cron service, use the / SBIN / Service CROND Start directive. To stop this service, you can use the / SBIN / Service Crond Stop instruction. It is recommended that you launch this service in the system.