Introduction to the Linux scheduled scheduling system cron

Source: Internet
Author: User
Introduction to the Linux scheduled scheduling system cron

Cron is a scheduled execution tool in Linux. It can run jobs without human intervention. This document does not introduce the implementation principle of cron. It mainly describes the specific use and brief introduction of cron.

New Scheduling taskWe recommend that you use the crontab-e command to add custom tasks (the corresponding user's Cron file under/var/spool/cron is edited). After exiting, restart the crond process, although the official documents describe "The crond command regularly checks every minute for jobs to be executed, it will be automatically executed if there are jobs to be executed. ", However, I once encountered a situation where I could not run the task without restarting (maybe because some systems read crontab in the memory, it was not executed immediately after/etc/crontab was modified ), you can restart the cron service or reload the cron configuration as follows: /Etc/rc. d/init. d/crond restart <br/> service cron reloadSimilarly, you can directly modify the/etc/crontab file to create a task. The content of the/etc/crontab file is as follows by default: Shell =/bin/bash <br/> Path =/sbin:/bin:/usr/sbin:/usr/bin <br/> mailto = root // if an error occurs, or there is data output, and the data is sent to this account as an email <br/> Home = // path of the user's operation, here is the root directory <br/> # Run-parts <br/> 01 ***** root run-parts/etc/cron. hourly // run/etc/cron hourly. hourly script <br/> 02 4 *** root run-parts/etc/cron. daily // run/etc/cron every day. daily script <br/> 22 4 ** 0 root run-parts/etc/cron. weekly // run/etc/cron every week. weekly script <br/> 42 4 1 ** root run-parts/etc/cron. monthly // run/etc/cron every month. monthly scriptsNote:
  • "Run-parts" is a parameter. If this parameter is removed, a script name to be run can be written later, instead of a folder name.
  • /Etc/cron. Hourly/,/etc/cron. daily/, cron. Weekly/,/cron. Monthly/stores the pre-defined cron tasks of the system.
The syntax of the/etc/crontab file is as follows:Minute hour day month dayofweek command
Minute hour day week command
The meaning of each field is as follows:
  • Minute executes the task in minutes of each hour.
  • Hour executes the task the hour of the day
  • Day of each month
  • Month: The month of each year.
  • Dayofweek executes this task the day of the week
  • Command specifies the program to be executed

In these fields, except that "command" is a field that must be specified each time, other fields are optional fields, which can be determined as needed. For unspecified fields, use "*" to fill their positions.

At the same time, cron supports writing similar to regular expressions and supports the following special symbol definitions:
  • "*" Indicates the number in the value range,
  • "/" Indicates "every ",
  • "-" Indicates a number to a number,
  • "," Separate several discrete numbers
Example:
* Ls indicates that the LS command is executed every 5th minutes of an hour.
30 5 *** ls specifies to execute the LS command at every day
30 7 8 ** ls: Specify to execute the LS command at on the 8 th of every month.
50 7 * root run-parts/etc/cron. daily runs as root at every day. all executable files in the daily directory [Note: Run-parts parameter indicates that all executable files in the following directory are executed. If a script is executed separately, you do not need to add the Run-parts parameter.] Service Management
Service cron start // start the service <br/> service cron stop // close the service <br/> service cron restart // restart the service <br/> service cron reload // reload configuration  Command Options-U specifies a user,
-L list the task plans of a user
-R: delete a user's task
-E: edit a user's task, for example: Crontab-u // set a user's cron service <br/> crontab-l // list the details of a user's cron service <br/> crontab-r // Delete none user's cron service <br/> crontab-E // edit a user's cron Service  Task category
  • System work: the work to be performed periodically by the system, such as backing up system data and clearing the cache. system work is generally defined in the/etc/crontab file.
  • Personal Work: The work that a user regularly performs, such as clearing files 3 days ago
Cron User restrictionsWe can set limits for different users, such as allowing user1 to use the cron service, while user2 to use the cron service. You can edit the following two files to implement this policy:
  • /Etc/cron. deny the user recorded here cannot use the cron task
  • /Etc/cron. allow is recorded here. You can use cron tasks

NOTE: If neither file exists, all users can use the cron service by default.

Others:
  • /Var/spool/cron/This directory stores the cron services of all users
  • /Var/log/cron records cron running logs

 

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.