一、定时任务之at实现;
PS:本次操作以CentOS 7.5为实验环境;
1.1、at软件包说明
最小化安装,可能没有at或atq这个命令,at来自于软件包at,如果yum能用,直接:
yum -y install at
[root@node2 ~]# rpm -ql at
/etc/at.deny
/etc/pam.d/atd
/etc/sysconfig/atd
/usr/bin/at
/usr/bin/atq
/usr/bin/atrm
/usr/bin/batch
/usr/lib/systemd/system/atd.service
/usr/sbin/atd
/usr/sbin/atrun
/usr/share/doc/at-3.1.13
/usr/share/doc/at-3.1.13/ChangeLog
/usr/share/doc/at-3.1.13/Copyright
/usr/share/doc/at-3.1.13/Problems
/usr/share/doc/at-3.1.13/README
/usr/share/doc/at-3.1.13/timespec
/usr/share/man/man1/at.1.gz
/usr/share/man/man1/atq.1.gz
/usr/share/man/man1/atrm.1.gz
/usr/share/man/man1/batch.1.gz
/usr/share/man/man5/at.allow.5.gz
/usr/share/man/man5/at.deny.5.gz
/usr/share/man/man8/atd.8.gz
/usr/share/man/man8/atrun.8.gz
/var/spool/at
/var/spool/at/.SEQ
/var/spool/at/spool
要想运行at任务,必须要启动atd.service服务:
systemctl status atd.service 查看状态
systemctl start atd.service 启动atd服务;
systemctl enable atd.service 让atd服务器开机自动启动;
1.2、at和其他几个关联组件
at executes commands at a specified time.
在指定的时刻执行命令;
atq lists the user's pending jobs, unless the user is the superuser; in that case, everybody's jobs are listed.
The format of the output lines (one for each job) is: Job number, date, hour, queue, and username.
atq用来操作用户追加的任务列表。如果是管理员查看,普通用户的任务列表也会被列出来。
atrm deletes jobs, identified by their job number.
通过任务的编号来删除任务;
batch executes commands when system load levels permit; in other words, when the load average
drops below 0.8,or the value specified in the invocation of atd.
batch会让系统自行选择在系统资源较空闲的时间去执行指定的任务;
用法和at一样,这里不介绍了,工作中一般at的用的比较少,我们的重心放在
后面要讲的cron上。
1.3、at的选项和介绍
常见的选项有:
-l:查看作业队列,相当于atq
-f /PATH/FROM/SOMEFILE:从指定文件中读取作业任务,而不用再交互式输入;
-d:删除指定的作业,相当于atrm;
-c:查看指定作业的具体内容;
-q QUEUE:指明队列;
注意:作业执行结果是以邮件发送给提交作业的用户;
at的作业有队列,用单个字母表示,默认都使用a队列;
at的语法结构为:
at [OPTION]... TIME
TIME可以设定为以下的形式,非常灵活多变:
HH:MM [YYYY-mm-dd]
noon,midnight, teatime
tomorrow
now+#
UNIT:minutes, hours, days, OR weeks
比如:
now +5minutes 表示任务将在5分钟后执行;
now +1hours 表示任务将在1小时候执行;
now + 31days 表示任务将在1个月后执行;
now +1weeks表示任务将在1周后执行;
12:32 表示任务将在今天的12:32分的时候执行;
[root@node2 ~]# at now+5m