The Linux system is controlled by the cron (Crond) system service. Linux systems have a lot of planned work on them, so this system service is started by default. In addition, because the user can set up scheduled tasks themselves, the Linux system also provides a command for the user to control scheduled tasks: the crontab command.
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.
The task scheduling under Linux is divided into two categories, system task scheduling and user task scheduling.
System task scheduling: The work to be performed by the system periodically, such as writing 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:
Cat/etc/crontab
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
mailto=home=/
The first four rows are the environment variables that are 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.
What the crontab file means:
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.
Second, Crond service
Start the service
/bin/systemctl Start Crond.service
Iii. Configuring timed execution of Myshell scripts
1. First create the. sh file under a folder and grant permission to the permissions file 755 permissions
Here my sh file path is/data0/myshell/conllect5m.sh
Then give permissions to the file
So Myshell all the files below have 755 permissions
Then open the conllect5m.sh file with the VI command
2. Switch to the directory
CD /data0/myshell
editing files with VI
VI collect5m.sh
The code is as follows: The first line is required, the second line is the description, the third line configures the address of the Execution plan task
#!/bin/sh #descripion: Description wget http://www.baidu.com
After writing, enter ESC to switch to command mode, enter: Wq save Edit and exit
3. We switch to the directory/etc below
Edit File vi crontab using VI
Enter the following command
shell=/bin/bashpath=/sbin:/bin:/usr/sbin:/usr/binmailto=root# for details see Mans 4 crontabs# Example of job definition: #.----------------minute (0-59) # |.-------------hour (0-23) # | |.----------DAY of Month (1-31) # | | |.------- Month (1-12) or jan,feb,mar,apr ... # | | | |.----DAY of Week (0-6) (sunday=0 or 7) or sun,mon,tue,wed,thu,fri,sat# | | | | |# * * * * * * user-name command to be execute# perform a scheduled task every 5,10,15,20,30 minutes every day */5 * * * * root/data0/myshell/collect5m . SH#*/10 * * * * ROOT/DATA0/MYSHELL/COLLECT10M.SH#*/15 * * * * * ROOT/DATA0/MYSHELL/COLLECT15M.SH#*/20 * * * * * ROOT/DATA0 /MYSHELL/COLLECT20M.SH#*/30 * * * * root/data0/myshell/collect30m.sh
After writing, enter ESC to switch to command mode, enter: Wq save Edit and exit
Then execute the start crontab, command
/bin/systemctl Start Crond.service
After the above steps, the scheduled task configuration is complete
How to use crontab timed tasks on Linux