MLUG Bash Scripting Workshop 25/04/08
Prev Index Next

Making log entries and shutting down the server

If you are observant, after reading the last two pages you might be wondering where the variable TEST2 comes from as we have seen TEST1 and TEST3 but not TEST2.

If you are very observant you will have remembered the screenshots of the editors early in this workshop and have some sort of an idea about the missing TEST2

Autoshtdn tests uses ping to test for two workstations and gets two variables TEST1 and TEST2 then gets TEST3 when it checks for at jobs. What can I say? It's a small network using static IP's. I'd have to do it differently if it was big and using DHCP.

The snippet below will show what happens next:


if [ $TEST1 -eq 1 -a $TEST2 -eq 1 -a $TEST3 -eq 1 ] ; then
	echo -e "\nran autoshutdn script `date +%d-%b-%R`" >> /var/log/autoshutdn
	echo -e "rick=$TEST1, leila=$TEST2, box=$TEST3, shutting down now." >> /var/log/autoshutdn
	shutdown -h now
else
	echo -e "\nran autoshutdn script `date `" >> /var/log/autoshutdn
	echo -e "rick=$TEST1, leila=$TEST2, box=$TEST3, no shutdown now" >> /var/log/autoshutdn
fi

In the if statement above " -a " means " and " so it would read $TEST1 equals 1 and $TEST2 equals 1 ... etc.

If all three variables equal 1 then then the following two lines are echoed then redirected to a file named /var/log/autoshtdn and the the computer shuts down. If any of the three variables is not 1 there are two other lines echoed and then redirected to /var/log/autoshtdn.

Here's what the log looked like just before shutdown last night.


ran autoshutdn script Sun Apr 13 21:00:01 EST 2008
rick=2, leila=2, box=1, no shutdown now.

ran autoshutdn script Sun Apr 13 21:15:11 EST 2008
rick=2, leila=1, box=1, no shutdown now.

ran autoshutdn script Sun Apr 13 21:30:04 EST 2008
rick=2, leila=1, box=1, no shutdown now.

ran autoshutdn script Sun Apr 13 21:45:04 EST 2008
rick=2, leila=1, box=1, no shutdown now.

ran autoshutdn script 13-Apr-22:00
rick=1, leila=1, box=1, shutting down now.



Top