Linux OS timing Scheduling system cron in-depth introduction _unix Linux

Source: Internet
Author: User

Cron is a regular execution tool under Linux, you can run the job without human intervention, this document does not talk about the cron implementation principle, mainly about the specific use of cron and brief introduction.

New Schedule Task
It is recommended that you use the CRONTAB-E command to add a custom task (edited by the corresponding user's cron file under/var/spool/cron), and then restart the Crond process after exiting, although the official document describes as "the Crond command will periodically check for work to be performed every minute If there is work to be done, the work is performed automatically. ", but I have encountered a situation where the task cannot be run without restarting (perhaps because some system crontab is read in memory, not executed immediately after modifying the/etc/crontab), you can restart the Cron service or reload the cron configuration using the following methods

Copy Code code as follows:

/etc/rc.d/init.d/crond restart
Service Cron Reload

Similarly, we can also modify the/etc/crontab file directly to create a new task,/etc/crontab file content defaults to the following:
Copy Code code as follows:

Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root//If there is an error, or if there is data output, the data is sent to this account as mail
home=///user Run path, this is the root directory
# Run-parts
* * * * * root run-parts/etc/cron.hourly//hourly execution of scripts within/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily//daily execution of/etc/cron.daily scripts
4 * * 0 root run-parts/etc/cron.weekly//per week execute/etc/cron.weekly script
4 1 * * Root run-parts/etc/cron.monthly//monthly to execute/etc/cron.monthly script

Attention
• "Run-parts" This parameter, if you remove this parameter, then you can write a script to run the name, not the folder name.
/etc/cron.hourly/,/etc/cron.daily/, cron.weekly/,/cron.monthly/storage System preset cron tasks

The/etc/crontab file syntax is as follows:

Minute Hour Day Month dayofweek command
Minutes hours days month days per week order

each field represents the following meanings:
minute the first few minutes of each hour
hour a few hours a day to perform this task
Day to perform the task on the first day of the month
month a few months of the year to perform this task
DayOfWeek to perform the task on the first day of the week
command Specifies the program to execute

In these fields, except that the Command is a field that must be specified each time, the other fields are optional fields that can be determined as needed. For fields that are not specified, use "*" to fill their position.
At the same time, Cron supports writing similar to regular expressions and supports the following special symbolic definitions:

• "*" represents the number within the value range,
• "/" stands for "every",
• "-" represents from a number to a number,
• "," separate several discrete numbers
Examples are as follows:

Copy Code code as follows:

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
7 * * * Root run-parts/etc/cron.daily executes all executables in the/etc/cron.daily directory as root every 7:50 [Note: The Run-parts parameter indicates that all executables in the following directory are executed, and if a script is executed individually, there is no need to increase the run-parts parameter. ]

Service Management
Copy Code code as follows:

Service cron start/Start Services
Service cron stop//shutdown services
Service cron restart//restart services
Service cron Reload//Reload Configuration

command Options
-u Specifies a user,
-l List A user's task schedule
-R Delete a user's task
-e Editing a user's task
such as:
Copy Code code as follows:

Crontab-u//Set a user's Cron service
CRONTAB-L//list details of a user's cron service
Crontab-r//Remove cron service for no users
CRONTAB-E//Edit a user's cron service

Task Classification
• System work: The work that the system performs periodically, such as backing up system data, cleaning up the cache, the system work is generally defined in the/etc/crontab file
• Personal work: A job that a user regularly needs to do, such as cleaning up files 3 days ago

Cron User Restrictions
We can set limits on different users, such as allowing User1 to use cron services, while User2 does not allow cron services; You can implement this strategy by editing the following two files:
/etc/cron.deny users who are logged in here cannot use cron tasks
/etc/cron.allow users who are logged here can use cron tasks
Note: If none of the two files exists, the Cron service is available to all users by default

Other:
/var/spool/cron/The cron service for all users under this directory
/var/log/cron log information to record cron running

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.