1, Periodic scheduled tasks - crontab
1. Command introduction
Repeat the command operation specified by the user according to the preset time period; To execute this command, you need to run the daemon: crond, but now the system generally runs automatically.
Primary profile
Global configuration file: / etc/crontab
Default settings: / rtc/cron*/
User defined settings: / var/spool/cron / user
See: / etc/crontab for the contents of the global configuration file
. command format
3. Command options crontab
Edit recurring tasks
-e: Edit recurring tasks; open vi Editor, it is best to add absolute path -l: View recurring tasks
Modify recurring tasks
-e: open vi Edit to delete -r: Delete all tasks of the current user; Use with caution!
The super user root can manage other users through commands
-u user name -e -u user name -l -u user name -r
4. Examples
Copy / etc/hostname to the opt directory at 10:00 on the 10th of each month and view the task
First, query the absolute path of cp
crontab -e
crontab -l
Copy files at 16:00 every three days: 0 16 *3/ * * /usr/bin/cp -p/erc/hostname /opt
0 0 1 * *: 12 o'clock every day (0 stands for 12 o'clock)
Every day at 7:00 *: 50 *: 50
0 * * / 5 * *: every hour every 5 days
crontad -u zhangsan -e: create a task plan through the user zhangsan
2, One time scheduled task
1. Introduction to at command
The at command allows you to specify when the Linux system will run the script. The at command will submit the job to the queue and specify when the shell will run the job. At's daemon atd will run in background mode and check the job queue to run the job. The atd daemon checks a special directory on the system to get the jobs submitted by the at command. By default, the atd daemon checks the directory every 60 seconds. When there is a job, the running time of the job will be checked. If the time matches the current time, the job will be run
2. at command format
When using the at command, you must ensure that the atd process starts
at [ HH:MM] [yyyy-mm-dd] //Create a one-time task; If you create a file that already exists in the system within the task, the creation time of the file will be changed to the current time ctrl+d //End input of at command atq: Query one-time task: query the unfinished task before the completion time atrm id No.: delete a one-time task /var/spool/at/: Storage directory for scheduled tasks /var/spool/mail: When it is time to execute the task, it will not be displayed in the foreground, and the system will send an email mail,Enter storage directory query
3. Time setting
at allows the use of a rather complex set of methods for specifying time
at now +2min: 2 Execute the created task in minutes at teatime: The default is to perform the task at 4:00 p.m. that day at teatime +3day: The representative will be on duty at 4 p.m. in three days at noon: The default represents 12 noon of the day at May 30 2021:2021 The mission was carried out on May 30 at 3pm: At 3 p.m
4. Examples
Copy the network card configuration file to the / op directory at 8:00 p.m. two days later, and then copy the file at 12:00 p.m. three days later
/etc/sysconfig/network- scripts/ifcfg- ens33 / / network card configuration file
[root@localhost ~]$at 8pm +2day at> cp -p /etc/sysconfig/network-scripts/ifcfg-ens33 /opt/ens33.bak at> <EOT> job 11 at Sat May 15 20:00:00 2021 [root@localhost ~]$at noon +3day at> cp -p /opt/ens33.bak /etc/sysconfig/network-scripts/ifcfg-ens33 at> <EOT> job 12 at Sun May 16 12:00:00 2021
3, Schedule task crontab execution sh script
The experimental steps are as follows, which are illustrated with examples
Create a new directory (it can be your current project, if the execution result of. sh script is related to your project. If it is related to the system itself, it is recommended to put it in another location)
cd /opt
Create a shell in the new directory SH file content: echo "hello world"
vim hello.sh echo "hello world"
- Two ways to run script files
sh hello.sh[hello.sh Can not x Permission]
Run the script file using the absolute path:/opt/hello.sh[Need to be given executive authority] Give the file owner execution permission: chmod u+x hello.sh;
/opt/hello.sh It becomes an order
crontab -e: Open the list of scheduled tasks
Write content to scheduled task table
*/1 * * * * /opt/hello.sh (Content that needs to be given executable permission)
4, Schedule task crontab permission settings
Crontab by default, all users can perform crontab settings and scheduling tasks. If necessary, you can use the / etc / cron Add a user to deny the user from setting scheduled tasks, or create / etc / cron Allow to use the whitelist
Blacklist cron Deny Directory: / etc | grep "cron.deny"
When the system schedules a task
Find allow file – > find deny file
If the same user exists in the two files, the user is still allowed to create scheduled tasks because the allow permission is higher
The whitelist is not available by default. You need to create your own cron allow//vim cron. allow