office microsoft outlook manage tips Microsoft Windows 7 Ultimate 64-bit microsoft office final exam microsoft office turorials Microsoft Office Visio Professional 2007 microsoft mouse driver for windows xp windows media center microsoft english Microsoft Windows 7 Home Premium 64 Bit microsoft windows start up tone microsoft office xp pro with frontpage Microsoft Windows 7 Professional beta information microsoft office system office xp microsoft outlook sp3 vista Microsoft Office Outlook 2007 microsoft office for windows xp microsoft office x mac Microsoft Windows 7 Ultimate (32 bit) microsoft windows user microsoft office 2007 training video Microsoft Windows XP Professional SP3 32-bit microsoft office setup cannot continue microsoft remote tools framework windows Microsoft Windows 7 Professional 64 Bit microsoft office standard 2003 key generator microsoft windows media player upgrade Microsoft Office 2003 Professional microsoft office 2003 upgrade requirements microsoft windows me repair Microsoft Office Project Professional 2003 microsoft windows network not accessible
msgbartop
I will happily conduct a FREE basic web security scan for any genuine organization interested in my services to point out whether or not I can find vulnerabilities in your application. Just contact me.
Need a PHP Programmer, PHP staff or project manager? Contact me now.
msgbarbottom

09 Sep 09 Shell Return Codes – Ping Monitoring

BASH – The Bourne Again Shell amongst most if not all other shells allows each application to exit with a return code. Some shells and environments have limits on what range this integer can fall into. Something between 0 and 255 inclusive is always a safe bet. In BASH, the variable $? is populated with the return code of the last command to return control back to the shell. It is important to preserve the return code immediately after the application exits that we want to monitor, as subsequent commands will overwrite the variable. The ping tool returns 0 on success:

HOST=”192.168.1.5″
ping -c1 ${HOST} -q 2>&1 >/dev/null  #ping HOST once and do not print any output to the screen
RET=$?  #assign the return code to RET so we can preserve it for after the ‘if’
if [ ${RET} -eq 0 ]; then
#we were successful.
echo “We were successful”
else
#we weren’t successful
echo “Host ${HOST} failed ping monitoring on `date`” |mail -s “Uptime Monitoring” admin@example.com
fi

Now of course there are easier ways of achieving the above task, although I’ve laid out the script in this way hoping that the way I have laid it out illustrates capturing the code and preserving it beyond the ‘if’ that follows which would have overwritten it. Just as further illustration, calling ping invalid followed directly by echo $? shows a return code of ‘2′ – obviously the return code for such a failure. Calling echo $? again immediately after shows a return code of ‘0′ as the return code of ping was overwritten by the return code of the first echo statement. Bash builtins return codes to the shell as any other application would.

Tags: , , , , , , , , ,



Leave a Comment

You must be logged in to post a comment.