开发者

If Service Exists Condition

How would you check if a WIN32 开发者_StackOverflowservice exists and, if so, do some operation?


You can't do this in DOS, as DOS is not Windows and doesn't even have the concept of a "service".

In a Windows batch file you can use the sc command to find services:

sc query | findstr SERVICE_NAME

This will enumerate all services and yield their respective names.

You can search for a specific service with

sc query | findstr /C:"SERVICE_NAME: myservice"

Remember that this search is case-sensitive. You can add the /I switch to findstr to avoid that.


Off the top of my head, you can check if a specific service is running, as mentioned by bmargulies, using the "net" command, piping the result into "find". Something like the following would check if a service was running, and if so stop it. You can then start it without worrying about if it was already running or not:

net start | find "SomeService"
if ERRORLEVEL 1 net stop "SomeService"
net start "SomeService"

If you're using findstr to do a search, as some of the other answers have suggested, then you would check for ERRORLEVEL equal to 0 (zero)... if it is then you have found the string you're looking for:

net start | findstr "SomeService"
if ERRORLEVEL 0 net stop "SomeService"
net start "SomeService"

Essentially most DOS commands will set ERRORLEVEL, allowing you to check if something like a find has succeeded.


Just an addendum to the accepted answer. If you are looking to do other things than just restarting the service, and are looking to see if the service is installed.

sc query state= all | findstr /C:"SERVICE_NAME: MyService" 
if ERRORLEVEL 0 (**My Operation**)

In this case, the state= all is important since if the service is not started, it will be interpreted as not installed, which are two separate things.


I'm using the code below:

SC QUERY | FIND "SERVICE_NAME: MyService"
IF %ERRORLEVEL% EQU 0 NET STOP MyService

If MyService is not found, %ERRORLEVEL% will be set at 1 by FIND, else it will stay at 0. The instruction IF %ERRORLEVEL% EQU 0 allows you to test this last case and proceed with an operation on your service.

IF ERRORLEVEL 0 NET STOP MyService

will not work, because it executes the command if %ERRORLEVEL% is greater or equal to zero.

In a Visual Studio post build event, you have to put an:

EXIT 0

at the end because VS will detect that %ERRORLEVEL% != 0 and it will consider that the post build event has failed. Use this with care because this will hide all the errors in your command sequence.

With this trick, you can ignore the error and use this in your post build event to restart your service:

NET STOP MyService
NET START MyService
EXIT 0


Should not be success tested: "if (not) errorlevel 1" ??

In windows shell "if errorlevel #" means the errorlevel is # or higher, so "if errorlevel 0" is alway true.


How about using WMIC:

First list all processes, then grep your process name. No result will be printed if it does not exist.

wmic service |findstr "ProcessName"

Example:

C:\>wmic service |findstr "Search"
FALSE        TRUE        Windows Search


Though there's an answer using wmic you can also the wql syntax that the command comes with:

wmic service where "name like '%%32TimeXXX%%'" get /Format:Value

Here's a whole script that uses the query above

@echo off

setlocal 
:::::::::::::::::::::::::::::::::::
set "SERVICE_TO_FIND=32Time"
:: you can assign also an argument
rem set set "SERVICE_TO_FIND=%~1"
:::::::::::::::::::::::::::::::::::

set "Name="
for /f "usebackq tokens=* delims=" %%# in (
    `wmic service where "name like '%%%SERVICE_TO_FIND%%%'" get Name /Format:Value`
) do (
    for /f "tokens=* delims=" %%] in ("%%#") do set "%%]"
)

if not defined Name (
    echo service like %SERVICE_TO_FIND% not found
    rem exit /b 1
) else (
    echo service found [%Name%]
)

endlocal
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜