Linux scheduled execution tool cron

Source: Internet
Author: User

After more than a decade of development in Linux, many users are familiar with Linux. Here we will introduce cron's understanding in Linux and discuss it with you. Cron is a Linux scheduled execution tool that runs jobs without manual intervention. This document does not describe how cron works, this section describes how to use and briefly introduce the Linux scheduled execution tool cron.

New Scheduling task
We recommend that you use the crontab-e command to add the cron file of the corresponding user in/var/spool/cron. 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 the crontab in the memory and the/etc/crontab is not executed immediately after modification ), you can restart the cron service or reload the cron configuration as follows:

 
 
  1. /etc/rc.d/init.d/crond restart  
  2. service cron reload 

Similarly, 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
PATH =/sbin:/bin:/usr/sbin:/usr/bin
MAILTO = root // if an error occurs or data is output, the data is sent to this account as an email.
HOME = // the path of the user running, which is the root directory
# Run-parts
01 *** root run-parts/etc/cron. hourly // execute the script in/etc/cron. hourly every hour
02 4 *** root run-parts/etc/cron. daily // run the script in/etc/cron. daily every day.
22 4 ** 0 root run-parts/etc/cron. weekly // run the script in/etc/cron. weekly every week.
42 4 1 ** root run-parts/etc/cron. monthly // run the script in/etc/cron. monthly every month.

Note:
"Run-parts". If this parameter is removed, you can write the script name of the table to be run, instead of the folder name.
/Etc/cron. hourly/,/etc/cron. dail/, cron. weekly/, and cron. monthly/store the pre-defined cron tasks.
The syntax of the/etc/crontab file is as follows:
MinuteHour Day Month Dayofweekcommand
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.
305 *** ls specifies to execute the ls command at every day
307 8 ** ls: Specify to execute the ls command at on the 8 th of each month.
50 7 * rootrun-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
Service cron stop
Service cron restart
Service cron reload // start the new load configuration

Command Options
-U specifies a user
-L list tasks of a user
-R: delete a user's task
-E. edit a user's task.

For example:
Crontab-u // specify the cron service of a user
Crontab-l // list the details of a user's cron Service
Crontab-r // delete a user's cron Service
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 restrictions
We 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. The preceding section describes how to use the Linux scheduled execution tool cron.

  1. Easy understanding of Linux shutdown commands
  2. Describes how to enter and exit a Linux operating system
  3. Describe Linux operating system deficiencies and Development Trends
  4. Introduction to Linux Application Scope
  5. This gives you a better understanding of common Linux software.

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.