Linux commands (003)--Crontab

Source: Internet
Author: User

First, the preparation of knowledge

The task scheduling under Linux is divided into two categories: System task scheduling and user task scheduling.

(1), System task scheduling

System task scheduling is the work that the system performs periodically, such as writing cache data to hard disk, log cleanup, etc. There is a crontab file in the/etc directory, which is the configuration file for the system Task Scheduler.

# Vi/etc/crontab shell=/bin/bashpath=/sbin:/bin:/usr/sbin:/usr/binmailto=roothome=/# for details see Mans 4 crontabs# Example of Job definition:#.----------------minute (0-59) # |  . -------------Hour (0-23) # |  |  . ----------Day of Month (1-31) # |  |  |  . -------Month (1-12) OR jan,feb,mar,apr ... # | | |  |  ----Day of Week (0-6) (sunday=0 or 7) or sun,mon,tue,wed,thu,fri,sat# | | | | |  # * * * * * *  user-name command to be executed

The first four rows are the environment variables that are used to configure the Crond task to run, the shell variable specifies which shell the system will use, this is bash, and the second line of the path variable specifies the path to the System execution command. The third line of the mailto variable specifies that Crond's task execution information will be emailed to the root user, and if the value of the mailto variable is null, the task execution information is not sent to the user, and the home variable in line fourth specifies the home directory to use when executing the command or script.

(2), user task scheduling

User task scheduling refers to the work that users perform on a regular basis, such as user data backup. Users can use the Crontab tool to customize their own scheduled tasks. All user-defined crontab files are saved in the/var/spool/cron directory.

/etc/cron.deny   users listed in this file are not allowed to use the crontab command/etc/cron.allow  the users listed in the file are allowed to use the crontab command/var/spool/cron/ directory where all user crontab files are stored, named by user name
Second, crontab command

The crontab command is used to maintain the crontab file for the associated user. Take a look at how the man page describes the crontab command: "Crontab is a program that is used to mount, remove, and display the Crontab Daemon's task table. ”

Iii.. crontab file

In the crontab file created by the user, each line represents a task, each field of each row represents a setting, its format is divided into six fields, the first five is the time setting segment, and the sixth paragraph is the command segment to execute, in the following format:

Minute hour day Month Week command # time-of-day weekly order

which

    • Minute: Represents minutes, which can be any integer from 0 to 59.
    • Hour: Represents the hour, which can be any integer from 0 to 23.
    • Day: Represents a date, which can be any integer from 1 to 31.
    • Month: Represents the month, which can be any integer from 1 to 12.
    • Week: Represents the day of the week, which can be any integer from 0 to 7, where 0 or 7 represents Sunday.
    • Command: The commands to execute can be either system commands or script files that you write yourself.

In each of these fields, you can also use the following special characters

    • Asterisk (*): represents all possible values, such as the month field if it is an asterisk, the command action is executed monthly after the constraints of other fields are met.
    • Comma (,): You can specify a list range with a comma-separated value, for example, "1,2,5,7,8,9"
    • Middle Bar (-): An integer range can be represented by a middle bar between integers, such as "2-6" for "2,3,4,5,6"
    • Forward slash (/): You can specify the interval frequency of the time with a forward slash, such as "0-23/2", which is performed every two hours. A forward slash can be used with asterisks, such as */10, if used in the minute field, which means that it executes every 10 minutes.
Iv. crontab Services
/sbin/service crond Start//    start service/sbin/service crond stop     //shutdown service/sbin/service crond restart  //Restart service/sbin/ Service Crond Reload   
V. Examples
# Execute once every 1 minutes * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *# 8 o'clock in the morning to 11 *# 3rd and 15-minute 3,15 8-11 */2 * *# each Monday 8 o'clock in the morning to 11 3rd and 15 minutes to execute 3,15 8-11 * * # 21:30 execution command per night 30 21 * * *# 1 10, 4:4,545, 22nd of each month 4 1,10,22 * *# per Saturday, Sunday 1:101 0 1 * * 6,0 # Daily from 18:00 to 23:00 every 30 minutes 0,30 18-23 * * * # every Saturday night 11:000 23 * * 6  

Linux commands (003)--Crontab

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.