无限循环?检查两台打印机是不是在线或离线 - 如果关闭则发送电子邮件

Posted

技术标签:

【中文标题】无限循环?检查两台打印机是不是在线或离线 - 如果关闭则发送电子邮件【英文标题】:Indefinitely Looping ? Check Two Printers if Online or Offline - Send Email if Down无限循环?检查两台打印机是否在线或离线 - 如果关闭则发送电子邮件 【发布时间】:2021-08-02 01:03:53 【问题描述】:

我有这个无限循环的脚本——有两台打印机要检查一次——printer01 和printer02。如果它们关闭或关闭,则发送电子邮件。 为什么它在第一台printer01上无限循环?


#!/bin/ksh
c=1
while [[ $c -le 3 ]]; do
STATUS=$(lpstat -printer0$c| grep 'READY' | awk 'print $3')

if [[  ! $STATUS == "DOWN" ||! $STATUS == "OFF" ]] ; then

        #create email summary and send

        today=`date +%m%d%Y`

        echo "*** START EMAIL SUMMARY ***                                    " > /lsf10/monitors/lpstat_email_$today.txt
        echo "*                                                              " >> /lsf10/monitors/lpstat_email_$today.txt
        echo "* Date - `date`                                                " >> /lsf10/monitors/lpstat_email_$today.txt
        echo "*                                                              " >> /lsf10/monitors/lpstat_email_$today.txt
        echo "* Start time   => $s_time                                      " >> /lsf10/monitors/lpstat_email_$today.txt
        echo "* Current time => `date +%H:%M:%S`                             " >> /lsf10/monitors/lpstat_email_$today.txt
        echo "*                                                              " >> /lsf10/monitors/lpstat_email_$today.txt
        echo "* Printer email message will be here for you to modify         " >> /lsf10/monitors/lpstat_email_$today.txt
        echo "* Please investigate.                                          " >> /lsf10/monitors/lpstat_email_$today.txt
        echo "*                                                              " >> /lsf10/monitors/lpstat_email_$today.txt
        echo "*** END EMAIL SUMMARY ***                                      " >> /lsf10/monitors/lpstat_email_$today.txt

        `mail -s "Mobius Printer Down Alert!" "email1,email2" < /lsf10/monitors/lpstat_email_$today.txt`
fi
c = $c + 1
done
exit

【问题讨论】:

不要在mail 命令周围加上反引号:这将执行命令,替换输出,然后尝试将输出作为命令执行。。跨度> 【参考方案1】:

考虑在每个外部命令之后添加错误检查。

您可以使用ksh -x 运行您的脚本来查看您的错误,这将打开调试模式。您也可以通过在脚本中添加set -x 来做到这一点。

您的脚本正在循环,因为变量 $c 没有按照您的想法设置。

您可能需要检查每个外部命令的退出代码 ($?),还要检查 lpstat 返回的标准输出是否为空或任何错误,并验证邮件程序是否在 PATH 上并且可执行等等

尝试不同的方法,如下所示:

#!/bin/ksh
typeset +i c=1 
while (( c <= 2 )); do
    STATUS=$(lpstat -printer0$c| grep 'READY' | awk 'print $3')
    if [[  ! $STATUS == "DOWN" ||! $STATUS == "OFF" ]] ; then
        #create email summary and send

        today=`date +%m%d%Y`
        outfile=/tmp/lpstat_email_$today.txt  # use your own path

        echo "*** START EMAIL SUMMARY ***                                    " > $outfile
        echo "*                                                              " >> $outfile
        echo "* Date - `date`                                                " >> $outfile
        echo "*                                                              " >> $outfile
        echo "* Start time   => $s_time                                      " >> $outfile
        echo "* Current time => `date +%H:%M:%S`                             " >> $outfile
        echo "*                                                              " >> $outfile
        echo "* Printer email message will be here for you to modify         " >> $outfile
        echo "* Please investigate.                                          " >> $outfile
        echo "*                                                              " >> $outfile
        echo "*** END EMAIL SUMMARY ***                                      " >> $outfile

        `mail -s "Mobius Printer Down Alert!" "email1,email2" < $outfile`
    fi  
    let c=$(( c + 1 ))
done
exit

【讨论】:

以上是关于无限循环?检查两台打印机是不是在线或离线 - 如果关闭则发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

如何在asmack,android中让用户在线或离线[重复]

Android : 通过 SERVER 获取在线或离线状态

玩转 pip 虚拟环境和安装包,包括在线或离线安装各种package

使用 Flutter 和 Firebase 实现实时在线/离线状态

用 cin 检查用户整数输入会产生无限循环

如何在线保存/加载数据(使用 AJAX 和 JSON 存储数据)和离线(本地)