I have this script that is looping indefinitely - there are two printers to check once each - printer01 and printer02. If they are DOWN or OFF then send an email. Why is it looping indefinitely on the first 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
Consider adding error checking after each external command.
You can use ksh -x
to run your script to see your mistake, this turns on debugging mode. You can also do this by adding set -x
inside the script.
Your script is looping because the variable $c is not being set the way you think.
You might want to check the exit code ($?) from each external command, and also check that the stdout returned by lpstat
is not empty or any error, and verify that the mail program is on the PATH and executable etc.
Try a different approach, something like this:
#!/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