Cron plan task configuration method in linux (centos)

  
                  

1. The crontab command option

The code is as follows: #crontab -u <-l, -r, -e>

-u specifies a user -l lists a user Task plan -r delete a user's task -e edit a user's task

2. cron file syntax and writing

can be edited with crontab -e command, edited /Var/spool/cron corresponds to the user's cron file, you can also directly modify the /etc/crontab file. The specific format is as follows:

The code is as follows: Minute Hour Day Month Week command Minute Hour Day Month Week Order 0-59 0-23 1-31 1-12 0-6 command

Each field The meaning of the representative is as follows:

The code is as follows: Minute executes the task in the first few hours of each hour. Hour performs the task in the first few hours of the day. Day The day of the month executes the task Month The number of each year Perform this task for a month. DayOfWeek executes the task on the first few days of the week. 0 means Sunday. Command specifies the program, script, or command to be executed.

In these fields, except for “Command” All fields except the field are optional. For fields that are not specified, use “*” to fill their location.

3. The meaning of several special symbols

“*” represents the number within the range of values, “/” represents ” every ”, “-” Represents a number from a number, “, & rdquo; separate a few discrete numbers

4. About the cron configuration file /etc/crontab

The main configuration file for cron is /Etc/crontab, which includes the following lines:

The code is as follows: 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

The first four lines are variables used to configure the cron task runtime environment:

The value of the SHELL variable specifies the shell environment (here defaults to Bash shell); The PATH variable defines the program path used to execute the command; the output of the cron task is mailed to the username defined by the MAILTO variable. If the MAILTO variable is defined as a blank string (MAILTO=""), the email is Will not be sent; HO ME variables can be used to set the home directory to use when executing commands or scripts.

As shown in the run-parts section of the /etc/crontab file, it uses the run-parts script to execute in /etc/cron.hourly, /etc/cron.daily, /etc/cron. Scripts in the weekly and /etc/cron.monthly directories, which are executed hourly, daily, weekly, or monthly, according to the preset time. The files in these directories should be shell scripts and have executable permissions (chmod +x filename).

If a cron task needs to be executed according to the schedule, not hourly, daily, weekly, or monthly, it can be added to the /etc/cron.d directory. All files in this directory use the same syntax as in /etc/crontab.

After editing a user's cron settings, cron automatically generates a file with the same name as this user in /var/spool/cron. The cron information of this user is recorded in this file. Files cannot be edited directly, they can only be edited with crontab -e. After cron starts, it reads this file every time it is read, and checks if it wants to execute the commands inside. Therefore, you do not need to restart the cron service after this file is modified.

5. Common cron example reference

5 * * * * ls Specifies to execute the ls command every 5th hour of the hour 30 5 * * * ls Specifies 5:30 every day to execute the ls command 30 7 8 * * ls Specify 7:30 on the 8th of each month to execute the ls command 30 5 8 6 * ls Specify the ls command to be executed at 5:30 on June 8th of each year 30 6 * * 0 ls Specify 6 every Sunday: 30 Execute the ls command [Note: 0 means Sunday, 1 means week 1, and so on, it can also be expressed in English, sun means Sunday, mon means Monday, etc. ] 30 3 10,20 * * ls Execute the ls command at 3:30 on the 10th and 20th of each month [Note: “, & rdquo; used to connect multiple non-contiguous time slots] 25 8-11 * * * ls per day Execute the ls command at the 25th minute of 8-11 [Note: “-” used to connect consecutive time periods] */15 * * * * ls Execute the ls command every 15 minutes [ie 0 0 of each hour) 30 45 60 minutes to execute the ls command] 30 6 */10 * * ls Each month, the ls command is executed every 6 days at 6:30 [ie, the monthly 1, 11, 21, 31 is 6:30 Execute an ls command]

Execute all executables in the /etc/cron.daily directory as root at 7:50 every day. 50 7 * * * root run-parts /etc/cron.daily [ Note: The run-parts parameter indicates that all executable files in the following directory are executed

The 10th, 20th, and 30th minutes are output to /tmp/cron1.txt: 10,20,30 * * * * echo " 10, 20, 30 minutes output " >> /tmp/cron1.txt

Run a program every two hours as user lzw.me:

0 * /2 * * * lzw.me /usr/bin/somecommand >> /dev/null 2>&1

6. Special Tips

A, cron configuration file path

#vi /etc/crontab B, restart cron method

#/etc/rc.d/init.d/crond restart Usage : /etc/rc.d/init.d/crond {start

Copyright © Windows knowledge All Rights Reserved