日期:2014-05-16  浏览次数:20637 次

我使用过的Linux命令之at - 在指定时间执行一次任务

我使用过的Linux命令之at - 在指定时间执行一次任务

本文链接:http://codingstandards.iteye.com/blog/774574 ?? (转载请注明出处)

用途说明

at命令用于在指定时间执行一次任务。如果任务需要重复执行,需用crontab。

atq用于打印队列中的任务,atrm用于删除任务。

常用参数

at TIME 用于设定指定时间的任务,其中TIME有很多表达方式。

at -f job.sh TIME 在指定时间执行job.sh。

使用示例

示例一

[root@jfht ~]# date
2010年 09月 30日 星期四 21:11:07 CST
[root@jfht ~]# at now + 5 minutes
at> date
at> <EOT>
job 1 at 2010-09-30 21:16

过了六七分钟之后
[root@jfht ~]# date
2010年 09月 30日 星期四 21:19:58 CST
[root@jfht ~]#

奇怪,在21点16分时没有打印出当前的时间。

可能发送到邮箱了,使用mail命令检查一下。

[root@jfht ~]# mail
Mail version 8.1 6/6/93.? Type ? for help.
"/var/spool/mail/root": 20882 messages 1 new 20882 unread

&
Message 20883:
From root@localhost.localdomain? Thu Sep 30 21:16:00 2010
Date: Thu, 30 Sep 2010 21:16:00 +0800
From: root <root@localhost.localdomain>
Subject: Output from your job??????? 1
To: root@localhost.localdomain

2010年 09月 30日 星期四 21:16:00 CST

示例二

[root@jfht ~]# at -f ./job.sh now + 5 minutes
job 2 at 2010-09-30 21:42
[root@jfht ~]# atq
2?????? 2010-09-30 21:42 a root
[root@jfht ~]#

过了五分钟之后。

[root@jfht ~]# cat job.txt
2010年 09月 30日 星期四 21:42:00 CST

?

示例三

[root@jfht ~]# date
2010年 09月 30日 星期四 21:44:58 CST
[root@jfht ~]# at 5pm + 3 days /bin/ls
syntax error. Last token seen: /
Garbled time
在Linux宝库上的例子有错,命令行上不能直接跟任务命令行。
[root@jfht ~]# at 5pm + 3 days
at> <EOT>
job 3 at 2010-10-03 17:00
[root@jfht ~]# atq
3?????? 2010-10-03 17:00 a root
[root@jfht ~]# at 5pm + 2 weeks
at> <EOT>
job 5 at 2010-10-14 17:00
[root@jfht ~]# at 17:20 tomorrow
at> <EOT>
job 6 at 2010-10-01 17:20
[root@jfht ~]# at 23:59 12/31/1999
at> <EOT>
job 7 at 1999-12-31 23:59

此处时间在过去!!!
[root@jfht ~]# atq
6?????? 2010-10-01 17:20 a root
3?????? 2010-10-03 17:00 a root
5?????? 2010-10-14 17:00 a root

注意没有任务7。
[root@jfht ~]# at 10:40 10/1/2010
at> <EOT>
job 8 at 2010-10-01 10:40
[root@jfht ~]# atq
6?????? 2010-10-01 17:20 a root
8?????? 2010-10-01 10:40 a root
3?????? 2010-10-03 17:00 a root
5?????? 2010-10-14 17:00 a root
[root@jfht ~]#

[root@jfht ~]# atrm 3
[root@jfht ~]# atq
6?????? 2010-10-01 17:20 a root
8?????? 2010-10-01 10:40 a root
5?????? 2010-10-14 17:00 a root

问题思考

1. 能否修改已经设置好的任务?

2. 时间有哪些常用格式?

相关资料

【1】Linux宝库 Linux指令篇:日期时间排程--at 注:里面的例子有问题

【2】鸟哥的Linux私房菜 实际运作工作排程

【3】Linux 技巧: 用 cron 和 at 调度作业

?

返回 我使用过的Linux命令系列总目录

?