shell 用at命令来计划执行作业


at命令允许指定Linux系统何时运行脚本。at命令会将作业提交到队列中,指定shell何时运 行该作业。at的守护进程atd会以后台模式运行,检查作业队列来运行作业。大多数Linux发行 版会在启动时运行此守护进程。

atd守护进程会检查系统上的一个特殊目录(通常位于/var/spool/at)来获取用at命令提交的 作业。默认情况下,atd守护进程会每60秒检查一下这个目录。有作业时,atd守护进程会检查作业设置运行的时间。如果时间跟当前时间匹配,atd守护进程就会运行此作业。

当作业在Linux系统上运行时,显示器并不会关联到该作业。取而代之的是,Linux系统会将
提交该作业的用户的电子邮件地址作为STDOUT和STDERR。任何发到STDOUT或STDERR的输出都 会通过邮件系统发送给该用户。


使用e-mail作为at命令的输出极其不便。at命令利用sendmail应用程序来发送邮件。如 果你的系统中没有安装sendmail,那就无法获得任何输出,因此在使用at命令时,最好在脚本 中对STDOUT和STDERR进行重定向.

code

macname@localhost Desktop % cat test.sh 
#!/bin/bash
# Test using at command
#
echo "This script ran at $(date +%B%d,%T)" > test13b.out echo >> test13b.out
sleep 5
echo "This is the script's end..." >> test13b.out%                                                                    macname@localhost Desktop % 
macname@localhost Desktop % 

#创建at任务

macname@localhost Desktop % at -m -f test.sh now
job 1 at Mon Nov 30 22:17:11 2020
macname@localhost Desktop % 
macname@localhost Desktop % 

#查看at任务

macname@localhost Desktop % atq
1    Mon Nov 30 22:17:00 2020
macname@localhost Desktop % 

#删除at任务

macname@localhost Desktop % atrm 1
macname@localhost Desktop % 

 

添加多个at任务

macname@localhost Desktop % at -m -f test.sh 22:20
job 2 at Mon Nov 30 22:20:00 2020
macname@localhost Desktop % atq                   
2    Mon Nov 30 22:20:00 2020
macname@localhost Desktop % 
macname@localhost Desktop % at -m -f test.sh teatime
job 3 at Tue Dec  1 16:00:00 2020
macname@localhost Desktop % at -m -f test.sh tomorrow
job 4 at Tue Dec  1 22:21:00 2020
macname@localhost Desktop % atq
3    Tue Dec  1 16:00:00 2020
2    Mon Nov 30 22:20:00 2020
4    Tue Dec  1 22:21:00 2020
macname@localhost Desktop % 

 

 

 

 

 

 

 

 

 

posted @ 2020-11-30 22:32  anobscureretreat  阅读(407)  评论(0编辑  收藏  举报