Reference article: How to use the Crond command for Linux task scheduling process and considerations
I. Introduction of Crond
Concept
The concept of Crond and crontab is inseparable. Crontab is a command that is commonly used in Unix-and Unix-like operating systems to set instructions that are executed periodically. The command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution. The word derives from the Greek language Chronos (χρ?νο), which was originally meant to be time. And Crond is its guardian process.
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.
The task scheduling under Linux is divided into two categories, system task scheduling and user task scheduling.
L System Task Scheduling: The system periodically to perform the work, such as write cache data to the hard disk, log cleanup and so on. 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.HOURLY02 4 * * * Root run-parts/etc/cron.daily22 4 * * 0 root Run-parts/etc/cron.wee KLY42 4 1 * * Root run-parts/etc/ Cron.monthly The first four rows are the environment variables that are used to configure the Crond task to run, a shell variable specifies which shell the system will use, this is bash, the second line of the path variable specifies the path of the system execution command, and the third row mailto variable specifies that the Crond task execution information will be A sub-message is sent 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 in line fourth specifies the home directory to use when executing the command or script. The meaning of line sixth to Nineth is described in detail in the next section. There's not much to say here.
User Task scheduling: Users to perform regular 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.
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:
L-u User: Used to set a user's crontab service, for example, "-u ixdba" means to set the IXDBA user's crontab service, this parameter is usually run by the root user.
L 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.
L-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-l: Displays the contents of a user's crontab file, or displays the contents of the crontab file for the current user if the user is not specified.
L-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.
L-i: Give confirmation prompt when deleting 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:
L minute: Represents minutes, which can be any integer from 0 to 59.
L Hour: Represents the hour, which can be any integer from 0 to 23.
L Day: Represents a date, which can be any integer from 1 to 31.
L Month: Represents the month, which can be any integer from 1 to 12.
L Week: Represents the day of the week, which can be any integer from 0 to 7, where 0 or 7 represents Sunday.
L Command: The commands to execute can be system commands or script files that you write yourself.
In each of these fields, you can also use the following special characters:
L 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.
L comma (,): You can specify a list range with a comma-separated value, for example, "1,2,5,7,8,9"
L Middle Bar (-): You can use the middle bar between integers to represent an integer range, such as "2-6" for "2,3,4,5,6"
L forward slash (/): You can use a forward slash to specify the interval frequency of time, for example, "0-23/2" means to execute 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) Examples of crontab files
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 indicates that the Apache log operation is performed at 5:10 of the month of 5th, 10th, 15th, 20th, 25th and 30th.
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 to redirect the standard output to/dev/null first, Standard errors are then redirected to standard output, and because standard output is redirected to/dev/null, standard errors are redirected to/dev/null so that the log output problem is resolved.
(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.
How to use Linux task scheduling process crontab and considerations