Quantcast
Channel: Tom's Blog » Windows
Viewing all articles
Browse latest Browse all 10

Redundancy check reboot script

$
0
0

One thing every administrator should do is to make sure their Windows servers are as up to date as possible. The problem with this is that Winodws update sometime requires a reboot, obviously this would usually create some down time or would require the administrator to do some maintenance over night.
In theory all really important systems that need 100% uptime should have atleast 2 servers to ensure they are redundant. This then means you can install updates on one and reboot it and the other will take over. The simplest way to do this is set automatic updates to schedule installation and reboot automatically this is Number 4 in the Group Policy. Just make sure you set one to install updates on say a tuesday at 2am and the other on a thursday at 2am.
What if however by chance one server is down for some reason and the other reboots to install updates, this will mean down time as both servers will be unavailable. So lets control the updates ourselves by turning off automatic reboot.
So now we can create a script that pings the secondary server to see if it is up first. If the secondary server is up then reboot else don’t reboot. The Code for this batch script is:

set errorlevel = 0
set ipaddress= 172.20.0.222
ping %ipaddress% > Output.txt
findstr /c:"Lost = 0" Output.txt
if %errorlevel% == 0 goto Exists
if %errorlevel% == 1 goto DoesntExist
goto end

:Exists
ECHO. ******************************** >> Log.txt
ECHO. Computer Shutdown at %TIME% on %DATE% >> Log.txt
shutdown /r /f /t 0
goto end

:DoesntExist
ECHO. ******************************* >> Log.txt
ECHO. Computer Not Rebooted. %ipaddress% down at %TIME% on %DATE% >> Log.txt
echo stay alive
:end

First Set error level to 0 and set a variable (ipaddress) to the Ip address of the secondary server we wish to ping. Now ping the variable set above and then output the result of this ping to a text file “Output.txt” The next line searches the output text file for the string “Lost = 0″ this would mean that the other server is reachable and therefore is likely to be up. If the string “Lost = 0″ was in the output then the error level will be at 0 and if the string wasnt in the output file this will create an error and so the error level will be 1. If the error level is 0 then jump to Exists point in the script. This will then Output to a text file the time and date and then it will reboot. If the error level is 1 then jump to DoesntExist point in the script. This will then output the time and date to the log file but will not reboot. The reason for creating a log file is that you can always refer to it to make sure the script is working but also lets you see when the server has rebooted or hasnt.
Now all you need to do is set a scheduled task to run this script whenever you want to reboot your server.
So set automatic updates to install at 2am then you can set a scheduled task to run this script at 3am ensuring an up to date server whilst maintaining 100% uptime.

The post Redundancy check reboot script appeared first on Tom's Blog.


Viewing all articles
Browse latest Browse all 10

Trending Articles