Cron Scheduled Task configuration method in Linux (CentOS)

Source: Internet
Author: User
Tags chmod centos

1. crontab Command Options


The code is as follows:
#crontab-U <-l,-R,-e>

-u Specifies a user
-l List A user's task schedule
-R Delete a user's task
-e Editing a user's task

2. cron file syntax and spelling

You can edit it with the Crontab-e command, edit the cron file of the corresponding user under/var/spool/cron, or modify the/etc/crontab file directly. The specific format is as follows:


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

Each field represents the following meanings:


The code is as follows:
Minute the first few minutes of each hour
Hour a few hours a day to perform this task
Day of the month to perform this task
Month a few months of the year to perform this task
DayOfWeek the task on the first day of the week, 0 means Sunday
command specifies the program, script, or command to execute

In these fields, the other fields are optional, except that the Command is a field that must be specified. For fields that are not specified, use "*" to fill their position.

3. The meaning of several special symbols

"*" represents the number in the range of values,
"/" stands for "every",
"-" represents from a number to a number,
"," separated by several discrete figures

4. About Cron configuration file/etc/crontab

The primary 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
* * * * Root run-parts/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily
4 * * 0 root run-parts/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly

The first four lines are variables used to configure the Cron task's environment to run:

The value of the shell variable specifies the shell environment (this is the default bash shell); The path variable defines the path of the program used to execute the command, and the output of the cron task is mailed to the user name of the MAILTO variable definition, if the MAILTO variable is defined as a blank string (mailto= ""), the email will not be sent out; the home variable can be used to set up the master directory used 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 the/etc/cron.hourly,/etc/cron.daily,/etc/cron.weekly, and/etc/ Scripts in the Cron.monthly directory, which are executed on an hourly, daily, weekly, or monthly basis, according to the preset time. The files in these directories should be shell scripts and have executable permissions (chmod +x filename).

A cron task can be added to the/ETC/CRON.D directory if it needs to be executed according to schedule instead of hourly, daily, weekly, or monthly. All files in the directory use the same syntax as in/etc/crontab.

After each edit of a user's cron settings, Cron automatically generates a file with the same name as this user under/var/spool/cron, and the user's cron information is recorded in this file, which is not directly editable and can only be edited with CRONTAB-E. After Cron starts, read the file every time it is written, and check if you want to execute the commands inside. Therefore, you do not need to restart the Cron service after this file has been modified.

5. Common Cron Sample Reference

5 * * * * ls Specifies the first 5 minutes of every hour to execute the LS command
5 * * * ls specifies 5:30 daily execution of LS command
7 8 * ls specifies 7:30 points per month 8th to execute LS command
5 8 6 * ls specifies every June 8 5:30 execution of the LS command
6 * * 0 ls Specifies 6:30 execution of the LS command per Sunday [Note: 0 for Sunday, 1 for Week 1, and so on, can also be expressed in English, Sun said Sunday, Mon said Monday. ]
3 10,20 * * ls 10th and 20th 3:30 Execute ls command [note: "," used to connect multiple discontinuous periods]
8-11 * * * ls 25 minutes per day at 8-11 o ' hour to execute the LS command [note: "-" to connect consecutive periods]
*/15 * * * * ls is executed once every 15 minutes by the LS command [that is, the No. 0 15 30 45 60 minutes per hour executes the LS command]
6 */10 * * ls every month, every 10 days 6:30 execution of the LS command [that is, 1, 11, 21, 31st of every month, yes 6:30 executes the LS command once]

Executes every executable file in the/etc/cron.daily directory as root 7:50 daily
7 * * * Root run-parts/etc/cron.daily [Note: run-parts parameter indicates that all executable files in the following directory are executed

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

Run a program every two hours as a 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 the Cron method

#/etc/rc.d/init.d/crond restart
Usage:/etc/rc.d/init.d/crond {Start|stop|status|reload|restart|condrestart}

C, don't forget to change the script to executable

chmod +x filename

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.