获取at命令调度的作业的输出

获取at命令调度的作业的输出

Content #

当在Linux系统中运行at命令时,显示器并不会关联到该作业。Linux系统反而会将提交该作业的用户email地址作为STDOUT和STDERR。任何送往STDOUT或STDERR 的输出都会通过邮件系统传给该用户。

来看一个在CentOS发行版中使用at命令调度作业的例子: $ cat tryat.sh #!/bin/bash

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 $ at命令会显示分配给作业的作业号以及为作业安排的运行时间。-f选项指明使用哪个脚本文件。now指示at命令立刻执行该脚本。

注意:无须在意at命令输出的警告消息,因为脚本的第一行是#!/bin/bash,该命令会由bash shell执行。

使用email作为at命令的输出极不方便。at命令通过sendmail应用程序发送email。如果系统中没有安装sendmail,那就无法获得任何输出。因此在使用at命令时,最好在脚本中对STDOUT和STDERR进行重定向,如下例所示: $ cat tryatout.sh #!/bin/bash

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. $ 如果不想在at命令中使用email或者重定向,则最好加上-M选项,以禁止作业产生的输出信息。

From #

Linux命令行与shell脚本编程大全