Article directory
Linux scheduled tasks
Linux scheduled tasks are a mechanism for automating specified tasks. It can automatically execute specified commands or scripts at specific times or events. Linux scheduled tasks can be used for a variety of different tasks. 例如备份数据、定期清理日志、定时重启服务等
There are two main types of Linux scheduled tasks:
one is based on the system's built-in cron
services, and the other is based on the system's own at
services. Below we will introduce how to use these two scheduled tasks respectively.
at command
at is a scheduled task tool that relies on the atd service and is suitable for scheduling tasks that will end once executed.
For example: If you need to temporarily back up data files, etc. at 3 a.m., you can use at
If we want to use at one-time scheduled tasks, our Linux system must have a service responsible for this scheduled task, which is the atd service.
Some Linux versions will not open it by default and require us to manually open the activation service.
[root@csq ~]# systemctl start atd
[root@csq ~]# systemctl status atd # 查看是否运行
Syntax
at[parameter][time]
parameter | explain |
---|---|
-m | When the specified task is completed, an email will be sent to the user, even if there is no standard output |
-I | alias of atq |
-d | alias of atrm |
-v | Shows the time the task will be executed |
-c | Print the contents of the task to standard output |
-V | Show version information |
-q | Use specified queue |
-f | Read tasks from the specified file instead of reading from standard input |
-t | Submit tasks to be run as time parameters |
The specified time used by at is also more complicated
You can use the current time or the next day's time to represent it. For example:
- hh/mm (hours minutes)
- day/hh/mm (day hour minute)
- tomorrow (tomorrow)
You can use words to specify times. For example:
-
midnight
-
noon(noon)
-
teatime (tea time, 4 p.m.)
You can also use the 12-hour clock to express morning and afternoon:
-
AM(morning)
-
PM(afternoon)
You can also use the specific date when the command was executed
- month day
- yy/mm/dd(year, month, day)
The above introduction is all absolute time, you can also use relative time timing, which is absolutely efficient for the command that is about to be executed.
Format: now + count time-units
now is the current time
count is the number of times, days or hours, today, tomorrow to specify the time to complete the command
time-units: is the unit of time, which can be minutes, hours, days, weeks
the case
Print csq after one minute
[root@csq ~]# at now+1min
at> echo 'csq'
at> <EOT>
job 12 at Tue Mar 21 20:38:00 2023
Tomorrow at 17:00, create a file and write the current time into it
[root@csq ~]# at 17.00 tomorrow
at> date >> time.txt
at> <EOT>
job 15 at Wed Mar 22 17:00:00 2023
After the scheduled task is set, before the scheduled task is executed, we can use the atq command to check whether the system has executed the task.
[root@csq ~]# atq
15 Wed Mar 22 17:00:00 2023 a root
[root@csq ~]# at -l
15 Wed Mar 22 17:00:00 2023 a root
Display the content of tasks that have been set and completed
[root@csq ~]# at -c 15
Delete tasks that have been set and completed
[root@csq ~]# atrm 15
[root@csq ~]# atq
[root@csq ~]#
How AT works
Since it is a planned task, there should be a way to execute the task. So how is the planned task generated?
We use the scheduled task generated by the at command and write the scheduled task into the /var/spool/at directory in the form of a document. The scheduled task can wait for the atd service to be executed.
Not all users can perform at scheduled tasks, because if the host is attacked and cracked, hackers will implant many hacking programs in their computers. These programs may use some scheduled tasks to run to control your computer and gain access to you. information, so users who are not very safe, don't let them use the at command.
We can use the two files /etc/at.allow and /etc/at.deny to restrict the use of at.
After adding these two files, the working situation of at is as follows:
first find the file /etc/at.allow. Users written in this file can use at. Users who are not in this file cannot use it. at (even if not written in at.deny)
Since it is assumed that all users on the system are trustworthy, the system usually keeps an empty /etc/at.deny file, which means that everyone is allowed to use the at command (you can check this file yourself)
If you don't want some users to use at, just write that user's account into /etc/at.deny! Write one line for each account
If neither file exists, only root can use the at command.
Add the user chenshiren to the blacklist and prevent him from using the at command.
[root@localhost home]# vi /etc/at.deny
chenshiren
[root@localhost home]# su - chenshiren
[chenshiren@localhost ~]$ at
You do not have permission to use at.
Adding the user chenshiren to the whitelist allows him to use the at command
[root@localhost home]# vi /etc/at.allow
chenshiren
[root@localhost home]# su - chenshiren
[chenshiren@localhost ~]$ at now+5min
at> echo 'csq'
at> <EOT>
job 1 at Wed Mar 22 07:09:00 2023
[chenshiren@localhost ~]$ atq
1 Wed Mar 22 07:09:00 2023 a chenshiren
cron periodic scheduled tasks
The way the crond process submits tasks is different from that of at. crond needs to read configuration files and has a fixed file format. Files are managed through the crontab command.
First check whether the crond service is turned on. If not, install it.
systemctl status crond
Install crond service
yum install -y cronie
crond service starts
systemctl start crond && systemctl enable crond
cron tasks are divided into two categories
System scheduled tasks
System timing configuration file parsing/etc/crontab
[root@localhost home]# cat /etc/crontab
SHELL=/bin/bash # 指定shell
PATH=/sbin:/bin:/usr/sbin:/usr/bin # 指定了系统可执行文件路径
MAILTO=root # 指定了crond的任务执行信息将通过电子邮件发送给root用户,如果MAILTO变量的值为空,则表示不发送任务 执行信息给用户
....
User scheduled tasks
Tasks that users need to perform regularly, such as user data backup, regular email reminders, etc.
crontab command
The crontab command is used to submit and manage users' tasks that need to be executed periodically. It is similar to scheduled tasks under Windows. When the operating system is installed, this service tool will be installed by default and the crond process will be automatically started. The crond process will be started every minute. It will periodically check whether there is a task to be executed, and if there is a task to be executed, the task will be automatically executed.
Syntax
crontab(options)(parameters)
options | explain |
---|---|
-e | editEdit scheduled tasks |
-l | list to view scheduled tasks |
-r | Delete scheduled tasks and remove the /var/spool/cron/username file |
-u | Specify a user to perform tasks, and root can manage scheduled tasks for ordinary users. |
-i | Delete a scheduled task and prompt the user to confirm the deletion to avoid errors. |
parameter
crontab file: Specify the crontab file containing the tasks to be executed
Like the at command, you can set the user's command permissions.
/etc/cron.deny 该文件中所列用户不允许使用crontab命令
/etc/cron.allow 该文件中所列用户允许使用crontab命令
/var/spool/cron/ 所有用户crontab文件存放的目录,以用户名命名
crontab task format:
minute hour day month week command 顺序:分 时 日 月 周
-
minute represents the minute which can be an integer from 0 to 59
-
hour represents the hour and can range from 0 to 23 (0 represents 0 o'clock)
-
day means the date can be 1~31
-
month means the month can be 1~12
-
week represents the day of the week and can range from 0 to 7 (0 or 7 represents Sunday)
In each of the above fields, you can also use the following special characters
-
The asterisk * represents all possible values
-
Comma, you can specify a list range with comma separated values, for example, "1,2,5,7,8,9"
-
Center bar - You can use a center bar between integers to represent an integer range, for example, "2-6" means "2,3,4,5,6"
-
Forward slash / You can use forward slash to specify the time interval frequency. For example, "0-23/2" means to execute every two hours.
the case
Go to school at 8:30 every morning
30 08 * * * go to school
Executed every minute
* * * * *
Executed every hour
00 * * * *
Every minute bucket execution every Saturday and Sunday at 1pm and 2pm
* 13,14 * * 6,0