I need to executes different commands on different status of windows service.
example: (My windows service is OracleServiceXE)
if OracleServiceXE is RUNNING i want to execute command1
if OracleServiceXE is STOPPED i want to execute command2
i wrote below code for this. but it is not working properly. it executes commands in abc portion when OracleServiceXE is RUNNING
sc query "OracleServiceXE" | find /i "RUNNING"
IF ERRORLEVEL 1 then GOTO abc
IF ERRORLEVEL 4 then GOTO bcd
:abc
echo aa
PING 1.1.1.1 -n 1 -w 6000>nul
goto :EOF
:bcd
echo bb
PING 1.1.1.1 -n 1 -w 12000>nul
what is the wrong ?
When searching with find, 0 is the return value when found, 1 is the return value when not found.
You can check that with
echo %errorlevel%after the find command.Also,
if ... thenis not cmd syntax (perhaps 4nt?).So, it could be written:
Note also the order of
IF ERRORLEVELis important asIF ERRORLEVEL nmeansIF ERRORLEVEL >= n