Linux timed Tasks

Source: Internet
Author: User

Introduction

This article mainly introduces the usage of the Linux timed Task command crontab, crontab is the command of the timed task, and Crond is the starting service of the scheduled task. Crontab's scheduled tasks are divided into system tasks and user tasks, but there is not much difference between them.

Start

In CentOS, the Crond service is automatically started by default and can be started manually if not started, as follows.

On

System Task Scheduling

System timing tasks are generally used to perform some system-related operations, such as log cleanup, and the system's scheduled tasks can be set by editing the/etc/crontab.
    • Shell: Which shell the system will use

    • Path: The paths of the commands executed by the System timer task, do not think that the path and the system environment variables are the same, there is no relation between them, the path configured in the system environment variable cannot take effect here.

    • MAILTO: The user who receives the message, by default, is root, and the contents of the mailbox are saved in the "/var/spool/mail/root" file.

    • Home: The home directory of the system's scheduled tasks.

Note: The System timer task must specify the user before the command, the general System timer task will use the root user to execute, the system timer task can also configure the path of the timer task, but use "run-parts", for example, every hour to execute the timer task under the folder, User timed tasks cannot specify a folder.
XX * * * * Root run-parts/etc/cron.hourly

User Task Scheduling

The user's timer task can be created by executing the crontab command, a user will only generate a timer task file will be named after the user name, user-defined timing tasks are saved in the "/var/spool/cron" directory, user task scheduling and system task scheduling time is the same,The only difference is that user task scheduling does not require a user name, and the directory cannot be specified using the Run-parts parameter.

crontab command Options

crontab [-u user] File

crontab [-u user] [-e |-l |-r]

    • -e: Create and edit timed tasks, default to create and edit the current user's scheduled tasks, and only root can create and edit other users ' scheduled tasks.

    • -L: Lists the current user's scheduled tasks, and if you specify the user with the-u parameter, the scheduled tasks for the specified user are listed.

    • -R: Deletes the current user's scheduled task, if the user is specified with the-u parameter, then it deletes the specified user's scheduled task.

    • -I: Prompt before deleting user commands, the-I parameter cannot be used alone before using with the-R parameter.

Crontab Task Format

Crontab have five time periods, separated by spaces as follows:
Minute hour day Month Week command order: time-sharing week

which

    • Minute: Represents minutes, which can be any integer from 0 to 59.

    • Hour: Represents the hour, which can be any integer from 0 to 23.

    • Day: Represents a date, which can be any integer from 1 to 31.

    • Month: Represents the month, which can be any integer from 1 to 12.

    • Week: Represents the day of the week, which can be any integer from 0 to 7, where 0 or 7 represents Sunday.

    • Command: The commands to execute can be either system commands or script files that you write yourself.

In each of these fields, you can also use the following special characters:

    • Asterisk (*): represents all possible values, such as the month field if it is an asterisk, the command action is executed monthly after the constraints of other fields are met.

    • Comma (,): You can specify a list range with a comma-separated value, such as: "1,2,5,7,8,9", and if it is month, the command is executed for several months.

    • Middle Bar (-): An integer range can be represented by a middle bar between integers, such as "2-6" for "2,3,4,5,6"

    • Forward slash (/): You can use a forward slash to specify the time interval frequency, such as "10-23/2" if the hour represents 10 points to 23 points every 2 hours. For example, */1 can be executed every one hours.

Other restrictions:

/etc/cron. deny the user listed in the file is not allowed to use the crontab command /etc/cron.  /var/spool/cron/directory where all users crontab files are stored, named after the user name

Note: The default user's shell,path,mailto,home is not defined, you can define the user's variables separately otherwise the user may not find some user software commands, the specific definition method can refer to the definition of the system task, mainly the shell, path. The default user information for these variables is as follows:

X-cron-env:<shell=/bin/sh>
X-cron-env: <HOME=/home/chen>
X-cron-env: <PATH=/usr/bin:/bin>
X-cron-env: <LOGNAME=chen>
X-cron-env: <USER=chen>

For example, I define a user timed task:

shell=/bin/bashpath=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/binmailto=  Chenhome=/data/backup*/5 01-06,13-18 * * *./userbackup.sh test

The above defines 1 to 6 points per day and 13 to 18 points, and executes commands every 5 minutes.

Note: Time range 01-06 refers to the 01:00-06:59

Summary

Timing task is very practical, but also a lot of practical methods, interested can be studied slowly.

Note:

pursuer.chen

Blog:http://www.cnblogs.com/chenmh

This site all the essays are original, welcome to reprint, but reprint must indicate the source of the article, and at the beginning of the article clearly give the link.

Welcome to the exchange of discussions

Linux timed Tasks

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.