无限循环?如果联机或脱机检查两台打印机 - 如果关闭则发送电子邮件

问题描述

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


#!/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

解决方法

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

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

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

您可能想要检查每个外部命令的退出代码 ($?),还要检查 lpstat 返回的 stdout 是否为空或任何错误,并验证邮件程序是否在 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