Cron is a daemon that can be used to schedule execution of repetitive tasks based on a combination of time, date, month, and week.
Cron assumes that 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 you must be running the crond service. To determine if 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.
Ii. Configuring Cron Tasks
The main configuration file for cron is /etc/crontab, which includes the following lines:
shell=/bin/bash PATH=/sbin:/bin:/usr/ sbin:/usr/bin MAILTO=root home=/ # run-parts * * * * root run-parts/etc/cron.hourly//hourly execution &nbs p; 4 * * * Root run-parts/etc/cron.daily//daily execution of scripts within/etc/cron.daily      &N bsp; 4 * * 0 root run-parts/etc/cron.weekly//weekly execution /etc/cron.weekly within Scripts 4 1 * * Root RUN-PARTS/ETC/CRON.MONTHL Y//Monthly go to execute script within/etc/cron.monthly |
The first four rows are variables that you can use to configure the Cron task runtime environment. The value of the shell variable tells the system which shell environment to use (in this case, the bash Shell), and the path variable defines the paths used to execute the command. The output of the cron task is mailed to the user name defined by the MAILTO variable. If the MAILTO variable is defined as a blank string (mailto= ""), the e-mail message is not sent. The home variable can be used to set the home directory to use when executing a command or script.
Attention to the "run-parts" parameter, if you remove this parameter, you can write a script name to run, instead of the folder name
Each line in the /etc/crontab file represents a task, in the form of:
Minute hour Day month DayOfWeek command |
minute -minutes, any integer from 0 to 59
hour -hours, any integer from 0 to 23
Day-date, any integer from 1 to 31 (if a month is specified, it must be a valid date for that month)
Month-months, any integer from 1 to 12 (or use the English abbreviations of the month such as Jan, Feb, etc.)
DayOfWeek -week, any integer from 0 to 7, where the 0 or 7 represents Sunday (or use the English abbreviations of the week such as Sun, Mon, etc.)
Command-commands to execute (commands can be commands such as Ls/proc >>/tmp/proc , or commands that execute scripts that you write yourself. )
In any of these values, an asterisk (*) can be used to represent all valid values. For example, an asterisk in a month value means that the command is executed monthly after other constraints have been met.
The short line between integers (-) specifies an integer range. For example,1-4 means integers 1, 2, 3, 4.
Specify a list with a series of values separated by commas (,). For example,3, 4, 6, 8 indicate these four specified integers.
A forward slash (/) can be used to specify the interval frequency. Adding /<integer after the range > means that integers can be skipped within the range. For example,0-59/2 can be used to define every two minutes in the Minutes field. The interval frequency value can also be used with asterisks. For example, the value of*/3 can be used in the month field to indicate that the task runs every three months.
The line starting with the pound sign (#) is a comment and will not be processed.
As you can see in the /etc/crontab file, it uses the run-parts script to execute /etc/cron.hourly,/etc/cron.daily , the/etc/cron.weekly , and the scripts in the /etc/cron.monthly directory that are executed hourly, daily, weekly, or monthly. The files in these directories should be shell scripts.
If a cron task needs to be executed according to the schedule instead of hourly, daily, weekly, or monthly execution, it can be added to the /etc/cron.d directory. All files in this directory use the same syntax as in /etc/crontab .
Users other than the root user can use the crontab tool to configure cron tasks. All user-defined crontab are saved in the /var/spool/cron directory and executed using the user who created them. To create a crontab project as a user, log on as the user, and then type the crontab-e command, using the editor specified by the VISUAL or editor environment variable to edit the user's Crontab The file uses the same format as the /etc/crontab . When a change to crontab is saved, the crontab file is saved according to the user name and written to the file /var/spool/cron/username .
The cron daemon examines changes in /etc/crontab files,etc/cron.d/ directories, and /var/spool/cron directories every minute. If a change is found, they will be loaded into memory. This way, you do not have to restart the daemon when a crontab file changes.
Third, the control of the use of cron
/etc/cron.allow and /etc/cron.deny files are used to limit the use of cron. The two formats used by the control file are one user per line. None of the two files allow spaces. If the control file is modified, the cron daemon (crond) does not have to be restarted. Using control files is read every time a user adds or deletes a cron task.
The root user can always use cron regardless of what is specified in the control file.
If the cron.allow file exists, only the users listed in it are allowed to use cron, and the cron.deny file is ignored.
If the cron.allow file does not exist, all users listed in Cron.deny are prohibited from using cron.
Iv. Starting and Stopping services
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 the service at boot time.
Since Cron is a built-in service for Linux, you can start and shut down this service in the following ways:
/sbin/service Crond Start//Startup service
/sbin/service Crond stop//Shut down service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload Configuration
。。。。。
Crontab exists in the/etc directory cron.d,cron.daily,cron.weekly,cron.monthly,cron.hourly five directories and crontab,cron.deny two files.
Cron.daily is the job,cron.weekly that executes once a day is the job that executes once a week.
Cron.monthly is the job that executes once a month job,cron.hourly is performed once per hour.
CRON.D is a task that the system automatically needs to do on a regular basis, but it is not done by the hour, by day, by week, by month, and then it is placed under this directory.
If it is done by the hour, by day, by week, by month, you can put it under the corresponding directory in front.
V. Examples
Crontab-u Specify a user
CRONTAB-E: Do a text editor to set the time table, the default text editor is VI, if you want to use another text editor, please set the VISUAL environment variables first
To specify the use of that text editor (for example, Setenv VISUAL Joe)
Crontab-r: Delete the current schedule table
Crontab-l: List the current schedule
crontab file [-u user]-replaces the current crontab with the specified files.
$crontab-L lists the user's current crontab.
Example:
#每天早上7点执行一次/bin/ls:
0 7 * * */BIN/LS
In December, the/usr/bin/backup is performed every 3 hours in the morning from 6 to 12.
0 6-12/3 */usr/bin/backup
Monday to Friday every 5:00 send a letter to [email protected] :
0 * * 1-5 mail-s "HI" [email protected] </tmp/maildata
Every day of the month 0:20 midnight, 2:20, 4:20 .... Perform echo "haha"
0-23/2 * * * echo "haha"
Attention:
When the program is executed at the time you specify, the system will send you a letter showing what the program is doing, and if you do not wish to receive such a letter, please leave a blank in each line
After adding >/dev/null 2>&1 can be
To create a cron service for the current user
1. Type CRONTAB-E edit crontab service file
For example, the contents of the file are:
*/2 * * * */bin/sh/home/admin/jiaoben/buy/deletefile.sh
Save the file and exit
*/2 * * * */bin/sh/home/admin/jiaoben/buy/deletefile.sh
*/2 * * * * This field allows you to set when to execute the script
/bin/sh/home/admin/jiaoben/buy/deletefile.sh This field can set the script you want to execute, note here that bin/sh refers to the path that the script holds after the command that runs the script
To see if the Crontab service under this user was created successfully, use the CRONTAB-L command
See if the service is already running with Ps-ax | grep cron
Linux crontab timed Execution Task command format with detailed examples