The Crond of the Linux wizard process

Source: Internet
Author: User


I. Introduction of Crond
Crond is a daemon that is used to periodically perform certain tasks or wait for certain events under Linux, similar to 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 is a task to perform and automatically executes the task if there are tasks to perform.

Two. Startup and shutdown of Cron

Since Cron is a built-in service for Linux, it can be started in the following ways. Close this service:

/sbin/service Crond Start//Startup service
/sbin/service Crond stop//Shut down service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload Configuration

The task scheduling under Linux is divided into two categories, system task scheduling and user task scheduling.

(1). System Task Scheduling: The work to be performed by the system periodically, such as write cache data to hard disk, log cleanup, etc. In the/etc directory there is a crontab file, this is the System Task Scheduler configuration file.


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 rows are the environment variables used to configure the Crond task to run:

The first line of the shell variable specifies which shell the system will use, and this is bash.

The second line of the path variable specifies the path to the System execution command.

The third line of the 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.

The four-row home variable specifies the home directory to use when executing the command or script.

Cron.daily is a job that executes once a day.

Cron.weekly is a job that executes once a week.

Cron.monthly is Jo that executes once a month.

Cron.hourly is the job that executes once per hour.

CRON.D is a task that the system automatically needs to do on a regular basis, but it is not performed by the hour, by day, by week, by month.

(2). User Task scheduling: the user periodically to perform work, such as user data backup, timed email reminders and so on. Users can use the Crontab tool to customize their own scheduled tasks. All user-defined crontab files are saved in the/var/spool/cron directory. Its file name is the same as the user name.


The cron service for Linux is to read all the content below/var/spool/cron,/etc/crontab,/etc/cron.d every minute.


Second, the use of crontab tools:
(1) crontab use format
Crontab commonly used in the following two types of formats:
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 Xiaoxiaohui" means to set Xiaoxiaohui user's crontab service, this parameter is usually run by the root user.
File:file is the name of the command file, which indicates that file is the Crontab task list and loaded into crontab. If this file is not specified on the command line, the crontab command will accept the commands typed on the standard input (keyboard) and load them into crontab.
-E: Edits the contents of a user's crontab file. If you do not specify a user, the crontab file for the current user is edited.
-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, if no user is specified, deletes the current user's crontab file by default.
-I: Give a confirmation prompt when deleting a user's crontab file.


(2) Meaning of the crontab file
In the crontab file created by the user, each line represents a task, each field of each row represents a setting, its format is divided into six fields, the first five is the time setting segment, and the sixth paragraph is the command segment to execute, in the following format:
Minute hour day Month Week command
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, for example, "1,2,5,7,8,9"
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 specify the interval frequency of the time with a forward slash, such as "0-23/2", which is performed every two hours. A forward slash can be used with asterisks, such as */10, if used in the minute field, which means that it executes every 10 minutes.


(3) Crontab Example:

Input command: CRONTAB-E

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7F/D3/wKiom1cu0aWyi4TtAABt2e37pfo095.png "title=" 2016-05-08 13-41-18 screen. png "alt=" Wkiom1cu0awyi4ttaabt2e37pfo095.png "/>

The function is to enter Hello in the hello file every minute.

The result is:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7F/D3/wKiom1cu0fLw7e8IAAAg4mAoY70961.png "title=" 2016-05-08 13-40-13 screen. png "alt=" Wkiom1cu0flw7e8iaaag4maoy70961.png "/>


Other Crontab file instances:

0 */3 * * */usr/local/apache2/apachectl restart
Indicates that the Apache service is restarted every 3 hours.
3 * * 6/webdata/bin/backup.sh
Represents an operation that executes the/webdata/bin/backup.sh script every Saturday 3:30.
0 0 1,20 * * FSCK/DEV/SDB8
Represents the 1th and 20th check/DEV/SDB8 disk devices per month.
5 */5 * * echo "" >/usr/local/apache2/log/access_log
Represents the 5th, 10th, 15th, 20th, 25th, and 30th of each month's 5:10 perform cleanup Apache log operations.

Iii. precautions for using the Crontab tool
(1) Pay attention to environmental variables.
Sometimes we create a crontab, but this task cannot be executed automatically, but it is not a problem to perform this task manually, which is usually caused by not configuring environment variables in the crontab file.
When defining multiple dispatch tasks in a crontab file, one of the issues that needs special attention is the setting of environment variables, because when we perform a task manually, it is done in the current shell environment, the program can certainly find the environment variable, and the system will not load any environment variables when it automatically executes the task schedule. Therefore, you need to specify all the environment variables that are required for the task to run in the crontab file, so that the system does not have a problem when it executes the Task Scheduler.
(2) Note the Mail log of the user who cleans the system
Each task is scheduled to execute, the system will send the task output information in the form of e-mail to the current system users, so the cumulative, log information will be very large, may affect the normal operation of the system, so it is important to redirect each task.
For example, you can set the following form in the crontab file, ignoring the log output:
0 */3 * * */usr/local/apache2/apachectl restart >/dev/null 2>&1
"/dev/null 2>&1" means that the standard output is redirected to/dev/null and then the standard error is redirected to standard output, and standard errors are redirected to/dev/null because the standard output has been redirected to/dev/null. This will solve the problem of log output.
(3) System-level task scheduling and user-level task scheduling
System-level task scheduling is mainly to complete some maintenance operations of the system, user-level task scheduling is mainly to complete the user-defined tasks, you can put the user-level task scheduling to the system-level task scheduling to complete (not recommended), but in turn, the root user's task scheduling operation can be through the "crontab– Uroot–e "To set, you can also write the dispatch task directly to the/etc/crontab file, it should be noted that if you want to define a scheduled restart of the system task, you must put the task into the/etc/crontab file, Even the task of creating a timed restart of the system under the root user is not valid.


This article is from "Narcissus" blog, please make sure to keep this source http://10704527.blog.51cto.com/10694527/1771191

The Crond of the Linux wizard process

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.