The Cron scheduled task configuration in Linux is detailed __linux

Source: Internet
Author: User
Tags chmod

Cron from the Greek word chronos (meaning "time"), refers to the Linux system next automatic execution of the specified task program (scheduled Task)

### #1. crontab command Options
The code is as follows:

#crontab-U <-l,-R,-e>-
u Specifies a user
-l List A user's task plan-
R Delete a user's task-
e Edit 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 hours days month week commands
0-59 0-23 1-31 1-12 0-6 command
each field represents the following meanings:

The code is as follows:

Minute a few minutes of the hour to perform the task Hour the first few hours of each day of the task to perform the task
Month The first few
months of the year
DayOfWeek The first day of the week to perform this task, 0 indicates that the 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 Files/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
01 * * * * * * Root run-parts/etc/cron.hourly
4 * * * * Root run-parts/etc/cron.daily
4 * * 0 root Run-parts/etc/cron.week LY
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 the hour to execute the LS command
5 * * ls specifies daily 5:30 execute LS command 7
8 * * ls specifies 8th per month 7:30 Execute LS command
30 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 the LS command [note: "," used to connect multiple discontinuous periods] 8-11 * * * ls, 8-11
minutes per day, the LS command [note: "-" used to connect consecutive periods of time]
  */15 * * * ls is executed every 15 minutes by the LS command [that is, the No. 0 15 30 45 60 minutes per hour of execution of the LS command]
6 */10 * * ls is executed once per month, every 10 days 6:30 (that is, 1, 11, 21, 3 per month) 1st Yes 6:30 executes the LS command
every 7:50 as root executes all executables in the/etc/cron.daily directory
7 * * *  root run-parts  /etc/ cron.daily [Note: The Run-parts parameter indicates that all executables in the following directory are
output to/tmp/cron1.txt in 10th, 20, and 30 minutes: 10,20,30 * * * * *
echo "10th, 20, 30-minute output ">>/tmp/cron1.txt
to 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

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.