Cron automatic scheduling service configuration details)

Source: Internet
Author: User

Cron is a built-in service in Linux. You can use the following methods to start or disable this service:

Reference:

/Sbin/service crond start // start the service

/Sbin/service crond stop // close the service

/Sbin/service crond restart // restart the service

/Sbin/service crond reload // reload the configuration

You can also enable the service automatically when the system starts:

Reference:

Add:

/Sbin/service crond start

Now that the cron Service is in the process, we can use it. The Cron Service provides the following interfaces for you to use:

1. directly use the crontab command to edit

The cron Service provides the crontab command to set the cron service. The following are some parameters and descriptions of this command:

Reference:

Crontab-u // set a user's cron service. Generally, the root user needs this parameter when executing this command.

Crontab-l // list the details of a user's cron Service

Crontab-r // Delete the cron service of no user

Crontab-E // edit a user's cron Service

For example, to view your cron settings as root:

Reference:

Crontab-u root-l

For example, Root wants to delete Fred's cron settings:

Reference:

Crontab-u Fred-R

When editing the cron service, the edited content has some formats and conventions. Enter:

Reference:

Crontab-u root-e

In the VI editing mode, the edited content must conform to the following format:

Reference:

*/1 ***** ls>/tmp/ls.txt

The first part of this format is the time setting, and the last part is the command to be executed. If there are too many commands to be executed, you can write these commands into a script, then you can directly call this script here. Remember to write the complete path of the command during the call. We have a certain agreement on the time setting. The first five * numbers represent five numbers. The value range and meaning of the numbers are as follows:

Reference:

Minutes

(0-59)

Hour

(0-23)

Date

(1-31)

Month

(1-12)

Week

(0-6) // 0 indicates Sunday

In addition to numbers, there are also several special symbols "-",",",*? Yi machinery na ≈ tamping section yi ne Zheng? "/" Indicates the meaning of each, "*/5" indicates every five units, "-" indicates the number from a number to a number, "," separate several discrete numbers. The following examples illustrate the problem:

Reference:

Every morning

0 6 *** echo "Good morning. ">/tmp/test.txt // note that no output is visible from the screen with pure echo, because cron has emailed any output to the root mailbox.

Every two hours

0 */2 *** echo "have a break now.">/tmp/test.txt

Every two hours from PM to am, am

0 23-7/2, 8 *** echo "have a good dream :)">/tmp/test.txt

Am from Monday 4 to Wednesday every week

0 11 4*1-3 command line

Am, January 1, January 1

0 4 1 1 * command line

After you edit the cron settings of a user, cron automatically generates a file with the same name under/var/spool/cron. The Cron information of this user is recorded in this file, this file cannot be edited directly. You can use crontab-e to edit it. The cron reads the file every minute after it is started, and checks whether to execute the commands in it. Therefore, you do not need to restart the cron service after the file is modified.

2. Edit the configuration cron in the/etc/crontab file.

The cron service not only reads all files in/var/spool/cron every minute, but also reads/etc/crontab once. Therefore, we can use the cron service to configure this file. Crontab configuration is intended for a user, and editing/etc/crontab is a system task. The file format of this file is:

Reference:

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 =/

# Run-Parts

01 *** root run-parts/etc/cron. Hourly // run the script in/etc/cron. Hourly every hour.

02 4 *** root run-parts/etc/cron. daily // run the script in/etc/cron. daily once a day.

22 4 ** 0 root run-parts/etc/cron. Weekly // run the script in/etc/cron. Weekly once a week.

42 4 1 ** root run-parts/etc/cron. Monthly // run the script in/etc/cron. Monthly every month.

User running path

Note the "run-parts" parameter. If this parameter is removed, you can write a script name to be run, instead of the folder name.

If you still don't understand it, read the following content:

Cron is a daemon that can be used to schedule the execution of repeated tasks based on the combination of time, date, month, and week.

Cron assumes that the system continues to run. If the system does not run when a task is scheduled, the task will not be executed.

To use the cron service, you must install the vixie-cron RPM package and run the crond service. To determine whether the software package is installed, run the rpm-Q vixie-cron command. To determine whether the service is running, run the/sbin/service crond STATUS Command.

The main configuration file of cron is/etc/crontab, which includes the following lines:

Code:

Shell =/bin/bash

Path =/sbin:/bin:/usr/sbin:/usr/bin

Mailto = root

Home =/

# Run-Parts

01 *** root run-parts/etc/cron. Hourly

02 4 *** root run-parts/etc/cron. daily

22 4 ** 0 root run-parts/etc/cron. Weekly

42 4 1 ** root run-parts/etc/cron. Monthly

The first four rows are variables used to configure the running environment of the cron task. The shell variable value tells the system which Shell environment to use (BASH Shell in this example); the PATH variable defines the path used to execute commands. The cron task output is mailed to the username defined by the mailto variable. If the mailto variable is defined as a blank string (mailto = ""), the email will not be sent. The Home variable can be used to set the home directory used for executing commands or scripts.

Each row in the/etc/crontab file represents a task. Its format is:

Code:

Minute hour day month dayofweek command

*

Minute-minute, any integer between 0 and 59

*

Hour-hour, any integer between 0 and 23

*

Day-date, any integer between 1 and 31 (if a month is specified, it must be the valid date of the month)

*

Month-month, any integer from 1 to 12 (or use the abbreviated month as Jan, Feb, and so on)

*

Dayofweek-any integer between 0 and 7 in a week. Here, 0 or 7 represents Sunday (or the abbreviation of a week, such as sun or Mon)

*

Command-yes? Are you sure you want to meet the requirements? Ls/proc>/tmp/proc and other commands can also be the command to execute your own script .)

In any of the preceding values, the asterisk (*) can be used to represent all valid values. For example, the asterisk in the month value means that this command is executed every month after other constraints are met.

The short line (-) between integers specifies an integer range. For example, 1-4 means integers 1, 2, 3, and 4.

Specifies a list of values separated by commas. For example, 3, 4, 6, and 8 indicate the four specified integers.

A forward slash (/) can be used to specify the interval. Adding/<integer> after the range means that integer can be skipped in the range. For example, 0-59/2 can be used to define every two minutes in the minute field. The interval value can also be used with the star number. For example, the value of */3 can be used in the month field to indicate that a task runs every three months.

The line starting with # Is a comment and will not be processed.

As you can see in the/etc/crontab file, it uses the Run-parts script to execute/etc/cron. hourly,/etc/cron. daily,/etc/cron. weekly and/etc/cron. scripts in the monthly Directory, which are executed hourly, daily, weekly, or monthly accordingly. The files in these directories should be shell scripts.

A cron task can be added to the/etc/cron. d directory if it needs to be executed hourly, daily, weekly, or monthly. All files in this directory use the same syntax as those in/etc/crontab. As follows:

Code:

# Record the memory usage of the system every Monday

# At 3: 30 AM in the file/tmp/meminfo

30 3 ** mon CAT/proc/meminfo>/tmp/meminfo

# Run custom script the first day of every month at 4:10 AM

10 4 1 **/root/scripts/backup. Sh

Users other than root users can use the crontab tool to configure cron tasks. All user-defined crontabs are saved in the/var/spool/cron directory and executed using the user identity to create them. Create a crontab project as a user, log on to the project as the user, type the crontab-e command, and edit the crontab in the editor specified by visual or editor environment variables. The format of the file is the same as that of/etc/crontab. After the changes made to crontab are saved, the crontab file is saved according to the user name and written to the file/var/spool/cron/username.

The cron daemon checks the/etc/crontab file, the etc/cron. d/directory, and the changes in the/var/spool/cron directory every minute. If changes are found, they are loaded into the memory. In this way, you do not have to restart the daemon after a crontab file is changed.

The/etc/cron. allow and/etc/cron. deny files are used to restrict the use of cron. The two control files are in the format of one user per line. Spaces are not allowed for both files. If the control file is modified, the cron daemon (crond) does not have to be restarted. The control file is read every time you add or delete a Cron task.

Regardless of the rules in the control file, the root user can always use cron.

If the cron. allow file exists, only the listed users are allowed to use Cron, And the cron. deny file is ignored.

If the cron. allow file does not exist, all users listed in cron. deny are forbidden to use cron.

To start the cron service, run the/sbin/service crond start command. To stop the service, run the/sbin/service crond Stop command. We recommend that you start the service during boot.

Add cron jobs

When we decide to regularly execute a command on the system, we must define the command to be executed as a cron job and define the cron job to be created in the crontab settings file, set the command execution time; or you can use cron to regularly execute/etc/Crom. * The Script feature of the directory. The command to be executed is created as a script file, which is stored in this directory. Pay attention to its permissions-it must be executable.

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.