使用at命令进行定时任务
1.安装at命令
[root@songyanling-node-1 gcinstall]# yum install at
2.启动atd服务
[root@songyanling-node-1 gcinstall]# systemctl start atd
[root@songyanling-node-1 gcinstall]# systemctl status atd
● atd.service - Job spooling tools
Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2023-11-22 14:02:46 CST; 5s ago
Main PID: 28635 (atd)
CGroup: /system.slice/atd.service
└─28635 /usr/sbin/atd -f
Nov 22 14:02:46 songyanling-node-1.novalocal systemd[1]: Started Job spooling tools.
3.使用at命令进行定时任务
可以用交互方式给at安排任务:
输入at命令并跟上具体时间,回车后进入交互界面,然后输入具体的命令,最后以ctrl+d结束输入。
[root@songyanling-node-1 gcinstall]# at 14:07
at> echo "123 >>/home/gbase/gcinstall/1.txt
at>
at>
job 4 at Thu Nov 23 14:07:00 2023
(这里4是该任务在任务列队里的id)
也可以用管道的方式将任务传递给at命令:
比如想60分钟后运行一个脚本:
echo “sh /opt/redmine_bug/test/run.sh &” | at now+60 minute
4.查看at任务列队
使用atq或者at -l
[root@songyanling-node-1 gcinstall]# at -l
1 Wed Nov 22 14:07:00 2023 a root
2 Wed Nov 22 14:07:00 2023 a root
3 Thu Nov 23 14:07:00 2023 a root
4 Thu Nov 23 14:07:00 2023 a root
查看任务内容使用: at -c id
[root@songyanling-node-1 gcinstall]# at -c 4
#!/bin/sh
#atrun uid=0 gid=0
#mail gbase 0
umask 22
GBASE_INSTANCES=/opt/10.10.13.222/gbase_profile; export GBASE_INSTANCES
……
……
……
${SHELL:-/bin/sh} << ‘marcinDELIMITER60a51daf’
echo "123 >>/home/gbase/gcinstall/1.txt
marcinDELIMITER60a51daf
5.删除某个任务
使用atrm命令
[root@songyanling-node-1 gcinstall]# atq
1 Wed Nov 22 14:07:00 2023 a root
2 Wed Nov 22 14:07:00 2023 a root
3 Thu Nov 23 14:07:00 2023 a root
4 Thu Nov 23 14:07:00 2023 a root
[root@songyanling-node-1 gcinstall]# atrm 4
[root@songyanling-node-1 gcinstall]# atq
1 Wed Nov 22 14:07:00 2023 a root
2 Wed Nov 22 14:07:00 2023 a root
3 Thu Nov 23 14:07:00 2023 a root