linux系统中at命令 一次性计划任务服务

1、一次性计划任务服务顾名思义只执行一次,直接测试

[root@linuxprobe test]# date  ## 查看系统当前日期
Mon Oct 19 12:45:10 CST 2020
[root@linuxprobe test]# at 12:48  ## 使用格式, at + 时间,表示在12:48执行命令
at> mkdir test1 test2
at> <EOT>    ## 这里使用ctrl + d 结束命令输入
job 2 at Mon Oct 19 12:48:00 2020
[root@linuxprobe test]# at -l   ## 这里列出所有额一次性计划任务服务
2       Mon Oct 19 12:48:00 2020 a root
[root@linuxprobe test]# at 12:50   ## 再创建一个一次性计划任务服务
at> echo "this is a test!"
at> <EOT>
job 3 at Mon Oct 19 12:50:00 2020
[root@linuxprobe test]# at -l  ## 查看当前系统中已经有两个一次性计划任务服务
2       Mon Oct 19 12:48:00 2020 a root
3       Mon Oct 19 12:50:00 2020 a root
[root@linuxprobe test]# atrm 3  ## 使用 atrm删除任务序号3
[root@linuxprobe test]# at -l  ## 再次查看只剩下一个计划任务服务
2       Mon Oct 19 12:48:00 2020 a root
[root@linuxprobe test]# ls
[root@linuxprobe test]# date  
Mon Oct 19 12:46:46 CST 2020
[root@linuxprobe test]# date  ## 查看系统日期
Mon Oct 19 12:48:11 CST 2020
[root@linuxprobe test]# ls  ## 已经多出两个目录,是一次性计划任务服务命令执行的结果
test1  test2
[root@linuxprobe test]# ll -h  ## 查看创建时间
total 0
drwxr-xr-x. 2 root root 6 Oct 19 12:48 test1
drwxr-xr-x. 2 root root 6 Oct 19 12:48 test2

[root@linuxprobe test]# at -l  ## 已经没有计划任务服务
[root@linuxprobe test]#

2、通过 at -f shell.sh time格式执行

[root@linuxprobe test]# ls
test.sh
[root@linuxprobe test]# cat test.sh  ## 测试脚本
#!/bin/bash
date
seq 10 > a.txt
date
[root@linuxprobe test]# date
Mon Oct 19 13:11:20 CST 2020
[root@linuxprobe test]# at -f test.sh  13:12  ## 设定时间执行
job 10 at Mon Oct 19 13:12:00 2020
[root@linuxprobe test]# at -l
10      Mon Oct 19 13:12:00 2020 a root
[root@linuxprobe test]# date 
Mon Oct 19 13:12:38 CST 2020
You have new mail in /var/spool/mail/root
[root@linuxprobe test]# ls  ## 命令已经执行
a.txt  test.sh
[root@linuxprobe test]# cat a.txt
1
2
3
4
5
6
7
8
9
10
[root@linuxprobe test]# ll -h  ## 时间戳一致
total 8.0K
-rw-r--r--. 1 root root 21 Oct 19 13:12 a.txt
-rw-r--r--. 1 root root 37 Oct 19 13:11 test.sh

3、时间格式采用12进制格式

[root@linuxprobe test]# ls
test.sh
[root@linuxprobe test]# cat test.sh
#!/bin/bash
date
seq 10 > a.txt
date
[root@linuxprobe test]# date
Mon Oct 19 13:16:47 CST 2020
[root@linuxprobe test]# at -f test.sh 01:18 PM  ## 指定下午
job 11 at Mon Oct 19 13:18:00 2020
[root@linuxprobe test]# ls
test.sh
[root@linuxprobe test]# date
Mon Oct 19 13:17:18 CST 2020
[root@linuxprobe test]# date
Mon Oct 19 13:17:59 CST 2020
[root@linuxprobe test]# date
Mon Oct 19 13:18:03 CST 2020
[root@linuxprobe test]# ls
a.txt  test.sh
[root@linuxprobe test]# ll
total 8
-rw-r--r--. 1 root root 21 Oct 19 13:18 a.txt
-rw-r--r--. 1 root root 37 Oct 19 13:11 test.sh
[root@linuxprobe test]# cat a.txt
1
2
3
4
5
6
7
8
9
10

4、时间格式 n 分钟后执行

[root@linuxprobe test]# ls
test.sh
[root@linuxprobe test]# cat test.sh
#!/bin/bash
date
seq 10 > a.txt
date
[root@linuxprobe test]# date
Mon Oct 19 13:20:32 CST 2020
[root@linuxprobe test]# at -f test.sh now+2 min
job 12 at Mon Oct 19 13:22:00 2020
[root@linuxprobe test]# ls
test.sh
[root@linuxprobe test]# date
Mon Oct 19 13:22:41 CST 2020
You have new mail in /var/spool/mail/root
[root@linuxprobe test]# ls
a.txt  test.sh
[root@linuxprobe test]# ll
total 8
-rw-r--r--. 1 root root 21 Oct 19 13:22 a.txt
-rw-r--r--. 1 root root 37 Oct 19 13:11 test.sh
[root@linuxprobe test]# cat a.txt
1
2
3
4
5
6
7
8
9
10

5、时间格式n天后执行

[root@linuxprobe test]# ls
test.sh
[root@linuxprobe test]# date
Mon Oct 19 13:24:47 CST 2020
[root@linuxprobe test]# at -f test.sh now+2 day
job 14 at Wed Oct 21 13:25:00 2020
原文地址:https://www.cnblogs.com/liujiaxin2018/p/13839780.html