I. Introduction
The crontab command schedules the execution of some commands at certain intervals. After the operating system is installed, the task scheduling command is started by default. The crond command periodically checks whether there is any job to be executed every minute. If there is any job to be executed, the job is automatically executed.
Ii. Parameters
-U: Specifies a user.
-L: list the task plans of a user.
-R: deletes a user's task.
-E: edit a user's task
Iii. cron File
1. Edit commands
1) Enter crontab-e in the command line and add the corresponding task. The wq disk will exit.
2) directly edit the/etc/crontab file, that is, vi/etc/crontab, and add the corresponding task
2. File Format
Minute Hour Day Month DayofWeek CommandPath
3. parameter description
Minute: the Minute of every hour to execute this task; value range: 0-59
Hour: the Hour of the day when the task is executed; value range: 0-23
Day: The Day of each month. The value range is 1-31.
Month: the number of months of the year to execute this task. value range: 1-12
DayOfWeek: the day of the week to execute this task; value range: 0-6, 0 indicates the weekend
CommandPath: Specifies the program path to be executed.
4. Time Format
*: Indicates any time point. For example, an hour * indicates an hour.
N: indicates a specific time. For example, if the hour is 5, it indicates 5 hours.
N, m: indicates a specific time. For example, an hour of 1 or 10 represents 1 and 10.
N-m: indicates a time period. For example, the hour is 1-5 and the hour is 1-5.
*/N: the number of time units at which the command is executed. For example, an hour */1 indicates that the command is executed every hour. It can also be written as 1-23/1.
5. Scheduling example
* 1 ***/opt/script/backup. sh: executed every 1 minute from.
15 05 ***/opt/script/backup. sh: 05: 15 run
*/10 */opt/script/backup. sh: run the command every 10 minutes.
0 17 ** 1/opt/script/backup. sh: executed at every Monday
2 8-20/3 ***/opt/script/backup. sh: 8: 02,11: 02,14: 02,17: 02,20: 02 run
6. Background execution
1) Description
When running a job on the foreground, the terminal is occupied by the job, but it does not occupy the terminal when running the job on the background. You can run the job in the background using the & command
Do not run jobs that need to interact with users in the background.
Jobs running in the background will output results to the screen. If jobs running in the background produce a large amount of output, it is best to redirect the output to a file.
2) ID
0: keyboard input
1: Standard output
2: Error output
&: Background execution
>: Overwrite the file content.
>>: Append object content
& 1: indicates the file description 1, indicating the standard output. If & is missing, it becomes number 1, indicating redirection to file 1.
2> & 1: redirect error output to standard output
1>/dev/null: redirects the standard output to the file/dev/null; 1>/dev/null is equivalent to>/dev/null
3) Example
0 2 ***/u01/test. sh 1>/dev/null 2> & 1 &
This statement means to execute this command in the background, redirect error output 2 to standard output 1, and then put all the standard output 1 to the/dev/null file, that is, empty.
4) Location
2> & 1 must be written after command 1> file.
In command 1> file 2> & 1, first, command 1> file redirects the standard output to file. 2> & 1 indicates that the standard output is copied due to a standard error, that is to say, it is also redirected to file, and the final result is that both the standard output and error are redirected to file.
If changed to: command 2> & 1> file 2> & 1, the standard error copies the standard output behavior, but the standard output is still on the terminal.> The output is redirected to the file, but the standard error is still on the terminal.