Linux scheduled task system Cron entry

Source: Internet
Author: User
Cron is a scheduled execution tool in linux. it can run jobs without manual intervention. because Cron is a built-in service in Linux, it does not automatically get up, you can use the following methods to start

Cron is a scheduled execution tool in linux. it can run jobs without manual intervention. because Cron is a built-in service in Linux, it does not automatically get up, you can start or disable the service in the following ways:

/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 automatically start the service when the system starts. Add:/sbin/service crond start at the end of the script/etc/rc. d/rc. local.

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:

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: crontab-u root-l

For another example, root wants to delete fred's cron settings: crontab-u fred-r

When editing the cron service, the edited content has some formats and conventions. enter crontab-u root-e.

In vi editing mode, the edited content must conform to the following format: */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:

Minutes (0-59)

Hour (0-23)

Date (1-31)

Month (1-12)

Week (0-6) // 0 represents Sunday

In addition to numbers, there are also several special symbols: "*", "/", "-", * representing all numbers in the value range, "/" 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:

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, the crontab configuration is intended for a user, while the/etc/crontab editing is intended for system tasks. the file format of this file is:

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 = // path of the user running. the root directory is used here.

# 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 // execute 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 the "run-parts" parameter. If this parameter is removed, you can write a script name to be run, instead of the folder name.

Related Article

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.