Use the at command to create a scheduled task

Before using the at command, you must first make sure it is installed on the system. The at command is installed by default in RHEL, but in centos, you need to install it manually.

Use the rpm -q at command to check whether there is an at command on your system

[root@localhost ~]# rpm -q at
at-3.1.13-20.el7x86_64

As shown in the picture, there is this installation package. If not, what should we do? Then we can use the yum install -y at command to install the at service.

[root@localhost ~]# yum -y install at
#省略输出信息,最终出现 Complete!,证明安装成功。

If the word "Complete" or "completed" appears as shown in the picture, the installation is successful. Successful installation alone is not enough. We also need to turn on the at service. And for the convenience of use, we also set up automatic startup at boot. (Since my system is centos8, the systemctl command is used here)

[root@localhost ~]# systemctl start atd && chkconfig atd on
注意:正在将请求转发到“systemctl enable atd.service”。
Created symlink /etc/systemd/system/multi-user.target.wants/atd.service → /usr/lib/systemd/system/atd.service.

 As shown in the picture, we have installed and started the atd service. Next, let’s start doing a few cases!

1. Require the server to shut down after 4 minutes from the current time, and send a reminder message "The server will shut down in two minutes" to all users who log in to the system.

First use the at command to set the startup time. The at now + minutes command means to execute the command in at after 2 minutes.

In at, we use the wall command to broadcast to all users who log in to the system.

Use the shutdown -h 2 command to specify the shutdown after 2 minutes.

Press the shortcut key ctrl+d to end at input. (Tips, if you don’t want to actually shut down, you can cancel the shutdown by entering shutdown -c in time after the shutdown command is executed)

[root@localhost ~]# at now +2 minutes
warning: commands will be executed using /bin/sh
at> wall The server will shut down in two minutes
at> shutdown -h 2
at> <EOT>
job 6 at Fri Apr 22 13:00:00 2022
[root@localhost ~]#

 2. Set the schedule to compress and package /var/log at 21:00 that day , and then shut down.

[root@localhost ~]# at 21:00
warning: commands will be executed using /bin/sh
at> tar -zcvf /var/log/log.tar.gz /var/log
at> shutdown -h now
at> <EOT>
job 12 at Fri Apr 22 21:00:00 2022
[root@localhost ~]# 

3. Use the atq command to query the waiting tasks, and use the atm (task number) command to cancel the waiting tasks.

[root@localhost ~]# atq
9	Fri Apr 22 13:26:00 2022 a root
[root@localhost ~]# atrm 9
[root@localhost ~]# atq
[root@localhost ~]# 

 The two small cases above are for reference.

Attached is another blog post. Detailed explanation of at command icon-default.png?t=M3K6http://c.biancheng.net/view/1090.html

Guess you like

Origin blog.csdn.net/qq_45477065/article/details/124357898