|
Answer» Hello Gurus,
I am very new to MS-DOS batch programming. My requirement is like this: I want to start a windows service and wait until it is completely started and display a message that it is running. I know that we can start a service USING SC command (sc start myservice), and check the status by sc query (sc query myservice).... But how to wait in a batch program until the service status changes to running.. My requirement is that, I need to wait until the service is UP and proceed further. I don't know how to KEEP checking the status of a service in a loop. If the status of the service changes to running then I should come out of the loop.
Does anybody know how to write this kind of batch program ?
Thanks in Advance.
Regards, Karbi
This is possible, just depends if you are going to run it on Windows XP(you'll need Tasklist: Link)
Code: [Select]@echo off <your service starting code here> tasklist /fi "SERVICES eq <Name of the service>" > services.txt finstr "<name of service>" services.txt
give this a try:
Code: [Select]@ECHO OFF CLS
REM Setting var SET SERVICE=[!!!!!your service name here!!!!!]
:SLOOP
IF EXIST "%TEMP%\SERVICE.TXT" DEL "%TEMP%\SERVICE.TXT"
ECHO Starting %SERVICE%..... ECHO.
SC START %SERVICE%>NUL
ECHO Testing for %SERVICE%..... ECHO.
SC QUERY %SERVICE% | FIND "STATE">%TEMP%\SERVICE.TXT
FOR /F "DELIMS==" %%A IN (C:\DOCUME~1\%USERNAME%\LOCALS~1\Temp\SERVICE.TXT) DO SET A=%%A& CALL :SERVICE %%A
GOTO EOF
:SERVICE
SET ERROR= ECHO %A% | FIND "RUNNING"&& SET ERROR=YES ECHO %A% | FIND "STOPPED"&& SET ERROR=NO
IF %ERROR%==YES GOTO YES IF %ERROR%==NO GOTO NO
:NO PING -n 1 -w 5000 1.1.1.1 > nul ECHO SERVICE %SERVICE% IS NOT STARTED..... GOTO SLOOP
:YES [!!!!!enter you command here!!!!!!] PAUSE Oh he's using SC not tasklist, thats understandableThanks Gurus for your replies...
I wrote it like this....Correct me if I am wrong...
@echo off sc start myservice > nul :begin sc query myservice > xxx findstr /i /m "running" xxx > nul if not %errorlevel%==0 ( goto begin )
echo myservice started
Regards, KarbiA variation
Code: [Select]@echo off cls
rem setting var set service=w32time
:sloop echo starting %service%..... echo. sc start %service%>nul echo testing for %service%..... echo. set error=no sc query %service% | find /i "state" | find /i "running" && goto yes ping -n 1 -w 5000 1.1.1.1 > nul echo service %service% is not started..... goto sloop :yes echo service %service% is running pauseHi Salmon,
Is it not enough to start the service just once ? I don't think it is required to start the service again and again, while checking for the status. I just modified your code a little bit.
@echo off cls
remsetting var set service=w32time
echo starting %service%..... echo. sc start %service%>nul echo testing for %service%..... :sloop echo. set error=no sc query %service% | find /i "state" | find /i "running" && goto yes ping -n 1 -w 5000 1.1.1.1 > nul echo service %service% is not started..... goto sloop :yes echo service %service% is running pauseQuote from: Karbi on July 29, 2009, 08:44:01 AM Hi Salmon,
Is it not enough to start the service just once ? I don't think it is required to start the service again and again, while checking for the status. Probably not; I had just tidied up wbrost's code somewhat without ALTERING the logic or structure.
|