Thursday, April 28, 2011

Service status check or delayed service start

During the years of administering different versions of Windows - I stumbled upon a problem where one windows/application service couldn't start because there was some prerequsites missing. Usually, one service is dependent on the other - for example: I had several cases where I for some reason need to reset the server and when server starts, some service fails because DB service isn't started yet. In these situations you usually end up in problems where application isn't working and you need to start service manually in order to fix the problem. This is why I find plain batch script very useful and it works for all windows versions. Below I wrote a script for postponing service startup for 5 minutes. Since, windows generaly doesn't have this tool installed - you should download tool sleep.exe from Microsoft web site, wich is part of Windows 2003 Resource Kit. Download resource kit and install it or unpack it and copy sleep.exe to some folder in system path.
Script goes something like:

@echo off
ECHO Delaying application service start for x minutes
sleep.exe 300
NET START "name of the application service"

Now, configure problematic service for a manual start and save this batch script and schedule it for a start at system boot. This will function as a delayed start of the service. This is feature already built into Windows 2008, but it doesn't exist in other windows versions.
To fine-tune a bit more, you should create this batch script and it's shortcut to configure this to start in minimized window so it doesn't obstruct users.

To go a little further, in cases where dependent services exist on the same computer, you should do the service status checking before doing some action of stopping or starting the service. For example - to check if one service is running you should include this in your script:

@echo off
ECHO Checking service status
NET START | FIND "name of the service1" > nul
IF errorlevel 1 GOTO :not_running
IF errorlevel 0 GOTO :is_running

:is_running
NET STOP "name of the service2"

:not_running
sleep.exe 300
NET START "name of the service2"


This script will do the service status checking and if service1 is running it will stop the service2 or if service1 is stopped it will do the delayed start of the service2. I believe you now understand what could you do with this.

Hope this helps. :)

No comments:

Post a Comment