Article Directory
1. One-time scheduled task at
Function: The scheduled task is mainly to do some periodic tasks. At present, the main purpose is to regularly back up data. All the output of the scheduled task execution will be sent to the specified user by email, unless redirected.
What is a scheduled task:
scheduled task , The literal meaning is to perform the planned work at the agreed time. In Linux, we often use cron services to complete this work. For example, we can use cron to back up a log file at 12 o'clock every night, which is a scheduled task.
One-time scheduling execution at
[root@at-cron ~]# rpm -qa |grep at
at.x86_64 0:3.1.13-24.el7
[root@at-cron ~]# yum -y install at
[root@at -cron ~]# systemctl start atd
[root@at-cron ~]# systemctl enable atd
software: at + time node
midnight: midnight (00:00)
noon: noon (12:00)
teatime: afternoon tea 4pm (14: 00)
23:59 12/31/2018 The task is at 23:59 on December 31, 2018
[root@at-cron ~]# at 23:59 12/31/2020
Create a scheduled task
at 11:00
rm -rf /tmp/*
ctrl +d ----> normal end
View the number of at planned tasks: [root@at-cron ~]# at -l
1 Tue Oct 27 11:00:00 2020 a root
2 Thu Dec 31 23:59:00 2020 a root
3 Tue Oct 27 00:00 :00 2020 a root
View detailed scheduled tasks:
[root@at-cron ~]# ls /var/spool/at (storage scheduled tasks directory)
a000010197dbf4 a0000201994c9f a000030197d960 spool
[root@at-cron ~]# cat /var/spool/at/a000010197dbf4
You can view the content of specific scheduled tasks
Delete the scheduled task:
[root@at-cron ~]# at -d 1 → work number
[root@at-cron ~]# at -r 2 → work number
[root@at-cron ~]# atrm 3 → work No.
delete / var / spool / at / directory below the corresponding files can also be a scheduled task
can be executed by looking at whether the log file to see
[root @ AT-cron ~] # tailf / var / log / messages
[AT-root @ cron ~]# echo 3> /proc/sys/vm/drop_caches clear cache
===============================================
2. Cyclic scheduled task cron
[root@at-cron ~]# rpm -qa |grep cron
crontabs-1.11-6.20121102git.el7.noarch
This service is self -started by the system by default, because the system-level task plan requires it. If it is not started, you need to start the crond service
[ root@at-cron ~]# systemctl start crond
[root@at-cron ~]# systemctl enable crond
[root@at-cron ~]# systemctl is-active crond
active
===User-level recurring tasks=
time+action
* * * * * command
.----------------minute (0-59) The first number of minutes
| .------------- (--230) -hour second represents the number of hours
| | .------------ day of month (1 - 31) The third representative of number of days
| | | .---- ------month (1-12) The fourth representative month
| | | | .--------day of week (0-6) (Sunday=0 or 7) The fifth* representative Day of the week
| | | | |
* * * * * command
* Means every...
, take a different time point
-means range
*/5 every 5 minutes (only every few minutes is meaningful)
* * * * * rm -rvf /mnt/* → /1 * * * * rm -rvf /mnt/
Create a recurring scheduled task (you can specify a user for a recurring scheduled task. If you do not specify it, the current user is the default)
[root@at-cron ~]# crontab -e
[root@at-cron ~]# crontab -e -u sunlizhen (Administrator You can use -u username, to manage other users' scheduled tasks)
View cyclic scheduled tasks
[root@at-cron ~]# crontab -l (User-level cyclic scheduled tasks crontab -l can directly view the content of the plan)
* * * * * rm -rvf /mnt/*
[root@at-cron ~ ]# crontab -l -u sunlizhen (Administrators view the cyclic scheduled tasks of specified users)
/1 * * * * rm -rvf /mnt/
Of course, you can also view the storage file of the cyclic scheduled task
[root@at-cron ~]# ls /var/spool/cron/
This directory uses the user as the file name to store the user-level cyclic scheduled task
root sunlizhen
[root@localhost ~]# cat /var/spool/cron/root
Delete recurring scheduled tasks
[root@localhost ~]# crontab -r (delete all recurring scheduled tasks of the current user)
[root@localhost ~]# crontab -r -u sunlizhen (administrator deletes all recurring scheduled tasks of the specified user)
If you don’t want to delete them all, you can edit them directly
[root@localhost ~]# crontab -e
[root@localhost ~]# crontab -e -u sunlizhen
permission control for scheduled tasks
[root@xingdian ~]# cat /etc/cron. deny
If this file exists, all accounts written in this file are not allowed to execute the crontab command
[root@xingdian ~]# cat /etc/cron.allow (this file does not exist by default)
If this file exists, it is not written The account in this file is not allowed to execute crontab command
If there is an allow file, then no matter whether deny exists or not, only users in the allow file are allowed
3. Watchdog view data copy backup cycle plan task example
①, vim edits a compressed and copied script
[root@at-cron ~]# vim /opt/kaobei.sh
#! /bin/bash
tar -czf kakaops`date +%y%m%d%H%M%S`.tar.gz /home/*
mv kakaops`date +%y%m%d%H%M%S`.tar.gz /mnt/
②, specify a scheduled task, execute the copy script every minute
[root@at-cron ~]# crontab -e
*/1 * * * * bash /mnt/kaobei.sh
③, watchdog checks the content changes in the specified directory
[root@at-cron ~]# watch -n 1 'ls -l /mnt'
-n Specify the number of seconds for the watchdog to update -n 1 See if the watchdog refreshes every second, you
can see that the patrol cycle scheduled task executes the copy script every minute
[root@localhost ~]# scp -r /etc 172.16.20.21:/tmp (remote copy)
[root@localhost ~]# du -h /home The size of the directory occupied by
date date display command
date displays the current date and time 1
date +%y%m%d%H%M%S
display mode (year, month, day should be lowercase (when the year is uppercase, it will display 2020), hour, minute, second should be uppercase, and any separator can be added in between)
date + %D display mode 3
date +%F display mode 4
date -s 9:43 Set the current time to 9:43