Batch file - please check my work...

September 19, 2011 at 07:38:48
Specs: Windows 2008 Stabdard SP1, n/a
Hi all,

I am trying to build a batch file for a server that will terminate running services in a specific order and then restart those services in reverse order.

Here is what I have:

STOP service 3

STOP /WAIT service 3
service 2

STOP /WAIT service 2
service 1

START /WAIT service 1
service 1

choice /Ca /T:a,30
START Service 2

choice /Ca /T:a,30
START Service 3

The 30 second pauses are so that each service is running before the next one starts. If there was a command that waited until a service was fully running I'd use that, but if there is a better way to make sure the services, which all need to work together and start in order, wait until the previous instance is running before it starts please let me know.


See More: Batch file - please check my work...

Report •

#1
September 19, 2011 at 08:38:34
You could use net start to start the services... have a 5 or 10 second timeout, and then do"

tasklist | find "service.exe" > tmpfile.txt

set /p variable=<tmpfile.txt

if defined variable goto next_service


Report •

#2
September 19, 2011 at 10:52:58
If service 2 and 3 are dependent on service 1 and 2 (respectively), and they're properly configured to signal that dependency, then you should be able to start service 1, 2, and 3 by starting service 3. You should also be able to bring all three down by stopping service 1.

How To Ask Questions The Smart Way


Report •

#3
September 19, 2011 at 12:48:20
You can use the sc command in cmd.

sc stop "yourservice"

http://ss64.com/nt/sc.html

I use for the wait command nircmd.exe

you can download here http://www.nirsoft.net/utils/nircmd...
scroll down on the website to see the download link.

nircmd wait "your desired miliseconds"


Report •

Related Solutions

#4
September 19, 2011 at 13:31:45
thanks everyone for your comments. Unfortunately the services do work on the same data which they pass around to each other, but none of them are configured to start any the other two. I also cannot add any software or run any other services on the server.

The complexity of the below is over my head. What exactly does it do?

You could use net start to start the services... have a 5 or 10 second timeout, and then do"

tasklist | find "service.exe" > tmpfile.txt

set /p variable=<tmpfile.txt

if defined variable goto next_service

I need to make sure enough time has elapsed for the previous service to be running, or if it possible - verified that it is running, before the next service is started.


Report •

#5
September 20, 2011 at 09:00:09
Hi SpiderSoup,

Here's the code again with comments and more detail and descriptive removed comments:

@echo off
SETLOCAL

:service1
REM Starting service1
net start service1.exe

REM Giving service1.exe 10 seconds to start
timeout 10

REM Using tasklist command piped to find to see if service1 is running
REM Also writing product of find to a temp file to use as a variable
tasklist | find "service1.exe" > tmpfile.txt

REM setting variable1 to check if service is started
set /p variable1=<tmpfile.txt

REM checking that variable1 is defined to make a decision to continue or start it again
if defined variable goto service2
REM if the above IF statement fails the next line will be read and service1 will start over
goto service1

:service2
REM Starting service2
net start service2.exe

REM Giving service2.exe 10 seconds to start
timeout 10

REM Using tasklist command piped to find to see if service2 is running
REM Also writing product of find to a temp file to use as a variable
tasklist | find "service2.exe" > tmpfile.txt

REM setting variable2 to check if service is started
set /p variable2=<tmpfile.txt

REM checking that variable2 is defined to make a decision to continue or start it again
if defined variable goto service3
REM if the above IF statement fails the next line will be read and service2 will start over
goto service2

:service3
REM Starting service3
net start service3.exe

REM Giving service3.exe 10 seconds to start
timeout 10

REM Using tasklist command piped to find to see if service3 is running
REM Also writing product of find to a temp file to use as a variable
tasklist | find "service3.exe" > tmpfile.txt

REM setting variable3 to check if service is started
set /p variable3=<tmpfile.txt

REM checking that variable3 is defined to make a decision to continue or start it again
if defined variable goto end
REM if the above IF statement fails the next line will be read and service3 will start over
goto service3

:end
REM deleting the temp file
del tmpfile.txt /q /f

ENDLOCAL


Report •

Ask Question