Thursday, November 18, 2010

Delay Starting Services with Batch File (Also use %errorlevel% and sc)

Recently I needed to delay starting up some services in Windows 2008 server. I had recently upgraded Veeam Backup and Replication from version 4 to version 5. When the server rebooted I found that for some reason three of the four of the services sort of hung. They said "starting" but would not finish starting. I found that if I started them up manually and started one at a time, they worked fine.

Now, I know that what I did is sort of a hack since the problem should not happen. Indeed after talking to Veeam tech support they said that they have never heard of the problem. Well, that still leaves me with a somewhat working system. I need it to work now, and then, I will try to scour for what the actual problem is. I could not leave it on manual because it would be easy to forget to start them up.

I needed to delay the start of the services, and stagger them so they would each start individually one after each other. There a few ways to delay start up of services. One is to create a dependency on another service. Another is to use a program that does that-the third for me was to write a script. I chose to write a script.

For my script to work, I needed another little utility: sleep.exe which is part of the Windows 2003 Resource kit.

Let's dissect it a bit. I am adding line numbers for easier reference, you would not have these line numbers.

* The first two lines are just comments
1. REM 1. VeeamNfsSvc - Veeam vPower NFS Service
2. REM Checking Service State

* Line 3 is very important. SC is a command line program used for communicating with the
Service Control Manager and services. If you want to find more about it in a command line window, just type sc /?.

*"sc" is the command. "query" is the command. In this case it says "look for". "VeeamNfsSvc" is the service name. "|" This is the Pipe. Pipe funnels the result to another command. In this case the "find" command. We are looking for the results of the query that has the text "STATE" in it, and we do it again and look for "RUNNING". The "> nul" redirects what you would normally see in the screen to nowhere. You will not see it. It is a dead end.

* A summary is look for a service called VeeamNfsSvc with the STATE of RUNNING (as opposed to STOPPED).

3. sc query VeeamNfsSvc | FIND "STATE" | FIND "RUNNING" > nul

* Lines 4 and 5. OK, this is decission time. When "sc" gets done it sets an exit code on the system if it was the code is 1 jump to label ":vPower_not_running. If it is 0, then jump to label "Veeam_Backup.

4. if %ERRORLEVEL% == 1 GOTO :vPower_no_running
5. if %ERRORLEVEL% == 0 GOTO :Veeam_Backup

* Line 6 through 9: Line 6 the label. Line 7 print message to screen. Line 8 uses sleep.exe to wait for 30 seconds. Then line 9 starts the service. Line 10 jumps to the next Label to test and start the next service if needed.

6. :vPower_no_running
7. echo Starting VeeamNfsSvc
8. sleep.exe 30
9. NET START VeeamNfsSvc
10.GOTO :Veeam_Backup

After this I went through each service doing the same thing. Four total services. To wrap the whole thing up, I added an Scheduled Task in Winodows Scheduler to start this script (which I saved as a *.bat file) at start up with administrator account. Also made sure that it runs whether the user is logged in or not. Why? Because if I reboot and I don't log in, it will still run and have the services started.


No comments: