run sc command to check service status

September 1, 2010 at 15:49:32
Specs: DOS, 512
I'm working on this batch file to run sc command to check service status which then sends it to a text file. From that file I look for a word "running". If it finds running, end the batch file but if running is not found, run another batch file. For some reason it runs the second batch file everytime so it doesn't tell me when the service is not running. This is what I got:

echo on

sc query signal >file.txt

sleep 3

find/i "running" file.txt

sleep 3

if exist "%running%" GOTO NOERROR
if not exist "%running%" GOTO ERROR


:NOERROR
ECHO NO ERRORS FOUND
GOTO END

:ERROR
ECHO Service not running
call Pager.bat
GOTO END


:END


See More: run sc command to check service status

Report •


#1
September 1, 2010 at 16:36:43
It could be that you are treating the find command as a filter command without the parameter. This shouldn't make any difference, but you never know with these things.

Example:

find /i "running" < file.txt

In any case it is a good check to see if your command is working by:

find /i "running" < file.txt > C:\somedir\itshere.txt

Also, separate the /i switch from the FIND command. Even try putting the full path to the text file. Leave nothing to chance.


Report •

#2
September 1, 2010 at 17:05:01
Another idea:

find /i "running" < file.txt > C:\somedir\running.dat

if exist C:\somedir\running.dat GOTO NOERROR
if not exist C:\somedir\running.dat GOTO ERROR

:NOERROR
ECHO NO ERRORS FOUND
DEL C:\somedir\running.dat
GOTO END


Report •

#3
September 2, 2010 at 04:03:34
Why not change it check for return codes?

IF ErrorLevel 1 GOTO ERROR
IF ErrorLevel 0 GOTO NOERROR

Or:
IF "%ErrorLevel%"=="1" GOTO ERROR
IF "%ErrorLevel%"=="0" GOTO NOERROR

Can I put a question here?


Report •

Related Solutions


Ask Question