shell_71.Linux定时运行作业

定时运行作业
1.使用 at 命令调度作业
(1)at 命令的格式
at 命令的基本格式非常简单:

at [-f filename] time


在默认情况下,at 命令会将 STDIN 的输入放入队列。可以用-f 选项指定用于从中读取命令(脚本文件)的文件名。

(2)at 命令能识别多种时间格式。
1) 标准的小时和分钟,比如 10:15。
2) b.AM/PM 指示符,比如 10:15 PM。
3) c.特定的时间名称,比如 now、noon、midnight 或者 teatime(4:00 p.m.)。

除了指定运行作业的时间,也可以通过不同的日期格式指定特定的日期。
1) 标准日期,比如 MMDDYY、MM/DD/YY 或 DD.MM.YY。
2) 文本日期,比如 Jul 4 或 Dec 25,加不加年份均可。
3) 时间增量。

Now + 25 minutes 
10:15 PM tomorrow 
10:15 + 7 days 


2.获取作业的输出
当在 Linux 系统中运行 at 命令时,显示器并不会关联到该作业。Linux 系统反而会将提交该作业的用户 email 地址作为 STDOUT 和 STDERR。
任何送往 STDOUT 或 STDERR 的输出都会通过邮件系统传给该用户。
来看一个在 CentOS 发行版中使用 at 命令调度作业的例子:

$ cat tryat.sh 
#!/bin/bash 
# Trying out the at command 
# 
echo "This script ran at $(date +%B%d,%T)" 
echo 
echo "This script is using the $SHELL shell." 
echo 
sleep 5 
echo "This is the script's end." 
# 
exit 
$ 
$ at -f tryat.sh now 
warning: commands will be executed using /bin/sh 
job 3 at Thu Jun 18 16:23:00 2020 
$ 

使用 email 作为 at 命令的输出极不方便。at 命令通过 sendmail 应用程序发送 email。
如果系统中没有安装 sendmail,那就无法获得任何输出。因此在使用 at 命令时,最好在脚本中对STDOUT 和 STDERR 进行重定向

$ cat tryatout.sh 
#!/bin/bash 
# Trying out the at command redirecting output 
# 
outfile=$HOME/scripts/tryat.out 
# 
echo "This script ran at $(date +%B%d,%T)" > $outfile 
echo >> $outfile 
echo "This script is using the $SHELL shell." >> $outfile 
echo >> $outfile 
sleep 5 
echo "This is the script's end." >> $outfile 
# 
exit 
$ 
$ at -M -f tryatout.sh now 
warning: commands will be executed using /bin/sh 
job 4 at Thu Jun 18 16:48:00 2020 
$ 
$ cat $HOME/scripts/tryat.out
This script ran at June18,16:48:21 
This script is using the /bin/bash shell. 
This is the script's end.' 
$ 


列出等待的作业
atq 命令可以查看系统中有哪些作业在等待:

$ at -M -f tryatout.sh teatime 
warning: commands will be executed using /bin/sh 
job 5 at Fri Jun 19 16:00:00 2020 
$ 
$ at -M -f tryatout.sh tomorrow
warning: commands will be executed using /bin/sh 
job 6 at Fri Jun 19 16:53:00 2020 
$ 
$ at -M -f tryatout.sh 20:30 
warning: commands will be executed using /bin/sh 
job 7 at Thu Jun 18 20:30:00 2020 
$ 
$ at -M -f tryatout.sh now+1hour 
warning: commands will be executed using /bin/sh 
job 8 at Thu Jun 18 17:54:00 2020 
$ 
$ atq 
1 Thu Jun 18 16:11:00 2020 a christine 
5 Fri Jun 19 16:00:00 2020 a christine 
6 Fri Jun 19 16:53:00 2020 a christine 
7 Thu Jun 18 20:30:00 2020 a christine 
8 Thu Jun 18 17:54:00 2020 a christine 
$ 


作业列表中显示了作业号、系统运行该作业的日期和时间,以及该作业所在的作业队列


4. 删除作业
一旦知道了哪些作业正在作业队列中等待,就可以用 atrm 命令删除等待中的作业。指定要删除的作业号即可:

$ atq
1 Thu Jun 18 16:11:00 2020 a christine 
5 Fri Jun 19 16:00:00 2020 a christine 
6 Fri Jun 19 16:53:00 2020 a christine 
7 Thu Jun 18 20:30:00 2020 a christine 
8 Thu Jun 18 17:54:00 2020 a christine 
$ 
$ atrm 5
$ 
$ atq
1 Thu Jun 18 16:11:00 2020 a christine 
6 Fri Jun 19 16:53:00 2020 a christine 
7 Thu Jun 18 20:30:00 2020 a christine 
8 Thu Jun 18 17:54:00 2020 a christine 
$ 


只能删除自己提交的作业,不能删除其他人的。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微辣已是极限

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值