Computer Hope

Microsoft => Microsoft DOS => Topic started by: Karbi on July 28, 2009, 06:37:58 AM

Title: Checking the status of the Service in a batch program
Post by: Karbi on July 28, 2009, 06:37:58 AM
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






Title: Re: Checking the status of the Service in a batch program
Post by: macdad- on July 28, 2009, 08:07:12 AM
This is possible, just depends if you are going to run it on Windows XP(you'll need Tasklist: Link (http://www.computerhope.com/download/winxp.htm))

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

Title: Re: Checking the status of the Service in a batch program
Post by: wbrost on July 28, 2009, 09:09:38 AM
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
Title: Re: Checking the status of the Service in a batch program
Post by: macdad- on July 28, 2009, 11:15:56 AM
Oh he's using SC not tasklist, thats understandable
Title: Re: Checking the status of the Service in a batch program
Post by: Karbi on July 28, 2009, 12:21:46 PM
Thanks 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,
Karbi
Title: Re: Checking the status of the Service in a batch program
Post by: Salmon Trout on July 28, 2009, 01:38:39 PM
A 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
pause
Title: Re: Checking the status of the Service in a batch program
Post by: 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. I just modified your code a little bit.

@echo off
cls

rem   setting 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
pause
Title: Re: Checking the status of the Service in a batch program
Post by: Salmon Trout on July 29, 2009, 10:08:09 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.