The use of Linux task scheduling process Crond

Source: Internet
Author: User

Introduction of Crond

Crond is a daemon that is used by Linux to periodically perform certain tasks or wait to handle certain events, similar to the Scheduled tasks under Windows, when the operating system is installed, the Service tool is installed by default, and the Crond process is started automatically. The Crond process periodically checks to see if there are any tasks to perform, and if there are tasks to perform, the task is automatically performed.

The task scheduling under Linux is divided into two types: System task scheduling and user task scheduling.

System task scheduling: The work to be performed periodically by the system, such as writing cache data to hard disk, log cleaning and so on. There is a crontab file in the/etc directory, which is the configuration file for system task scheduling.

The/etc/crontab file includes the following lines:

Shell=/bin/bash

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

Mailto=root

home=/

# Run-parts

* * * * Root run-parts/etc/cron.hourly

4 * * * Root run-parts/etc/cron.daily

4 * * 0 root run-parts/etc/cron.weekly

4 1 * * Root run-parts/etc/cron.monthly

The first four lines are the environment variables that are used to configure the Crond task to run, and a shell variable that specifies which shell the system will use, this is bash, and the second line path variable specifies the path to the System execution command. The third row mailto variable specifies that Crond's task execution information will be emailed to the root user, and if the value of the mailto variable is null, the task execution information is not sent to the user, and the home variable on line fourth specifies the master directory to use when executing the command or script. The meaning of line sixth to Nineth will be described in detail in the next section. It's not much to say here.

User Task scheduling: Tasks that users perform on a regular basis, such as user data backup, timed email reminders, and more. Users can use the Crontab tool to customize their scheduled tasks. All user-defined crontab files are saved in the/var/spool/cron directory. The file name is the same as the user name.

Second, the use of crontab tools

(1) The use format of crontab

Crontab commonly used formats are as follows: two.

crontab [-u user] [file]

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

The options have the following meanings:

-u User: Used to set a user's crontab service, for example, "-u ixdba" means setting the IXDBA user's crontab service, which is typically run by the root user.

File:file is the name of the command file, which means that file will be crontab as a task list and loaded into crontab. If this file is not specified on the command line, the crontab command accepts the commands typed on the standard input (keyboard) and loads them into crontab.

-E: Edit the contents of a user's crontab file. If you do not specify a user, edit the current user's crontab file.

-L: Displays the contents of a user's crontab file, or displays the contents of the current user's crontab file if no user is specified.

-r: Deletes a user's crontab file from the/var/spool/cron directory, and deletes the current user's crontab file by default if no user is specified.

-I: Give a confirmation prompt when deleting a user's crontab file.

(2) The meaning of crontab file

In the crontab file created by the user, each row represents a task, each field in each row represents a setting, its format is divided into six fields, the first five paragraphs are time set, and the sixth paragraph is the command segment to be executed, in the following format:

Minute hour day Month Week command

which

Minute: Represents a minute, which can be any integer from 0 to 59.

Hour: Represents an 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: Indicates the day of the week, can be any integer from 0 to 7, where 0 or 7 represent Sunday.

Command: The commands you want to execute, 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, indicates that the command operation is performed every month after the constraints of other fields are met.

Comma (,): You can specify a list range with a comma-separated value, for example, "1,2,5,7,8,9"

Medium Bar (-): You can use the middle bar between integers to represent an integer range, for example, "2-6" means "2,3,4,5,6"

Forward slash (/): You can use a forward slash to specify the interval frequency of the time, for example, "0-23/2" means to perform every two hours. The forward slash can also be used with the asterisk, such as */10, which, if used in the minute field, means that it is executed every 10 minutes.

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.