Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to stop and start a Windows service from a remote PC using the Windows command line, in a batch file.

sc \\192.168.1.1 stop <ServiceName>

rem sc \\192.168.1.1 query <ServiceName> | findstr STATUS | SET VAR=

However, I want to wait until the status of the service is assured to be stopped or started. So I was planning to loop and check the status continuously until the status is STOPPED.

I think that repeated request to the server may be processor intensive but there is no wait for the command line except for some hacks on checking time. But my main question is how do I get the status of the Windows service so I can check with an IF statement if it is OK to proceed to the next command?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

To set a variable to the output of a command, use for /f:

for /f "tokens=*" %%a in ('command') do set _CmdResult=%%a

The problem is, to use a pipe in the command you need to escape it with the command line escape character: ^, therefore: ^|.

for /f "tokens=*" %%a in ('sc \\192.168.1.1 query <ServiceName> ^| findstr STATUS') do set _CmdResult=%%a

Now, I'm not sure which version of Windows you're running, but my attempts at a sc query on Windows 7 give the following output:

>sc query SamSs
SERVICE_NAME: SamSs
        TYPE               : 20  WIN32_SHARE_PROCESS
        STATE              : 4  RUNNING
                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

This means a findstr STATE would be required, which gives the output:

>sc query SamSs | findstr STATE
        STATE              : 4  RUNNING

Now, in the example above, tokens=* means every token is set to the variable %%a, where tokens are separated by spaces. In this case, you can use either the third token (4) or fourth token (RUNNING). By the way, with testing, 1 corresponds to STOPPED for me.

To make things easier, I'll be using the string RUNNING in this example. So we want the fourth token.

for /f "tokens=4" %%a in ('sc \\192.168.1.1 query <ServiceName> ^| findstr STATE') do set _CmdResult=%%a

Of course, depending on what your sc query outputs, yours may be slightly different, so follow how I got there and modify as needed. Or post in a comment the output of your sc query and I'll modify as needed.

To check the value, you can use:

if %_CmdResult%==STOPPED (
    REM do something here
)

In your case, the loop would go something like this.

:loop
for /f "tokens=*" %%a in ('sc \\192.168.1.1 query <ServiceName> ^| findstr STATUS') do set _CmdResult=%%a
if %_CmdResult%==STOPPED (
    sc \\192.168.1.1 start <ServiceName>
    goto endloop
)
timeout /t 5
goto loop
:endloop

Note that timeout is a utility only included in Windows Vista and later. Windows XP can use something like ping 1.1.1.1 -n 1 -w 5000 > nul.

share|improve this answer
    
Thanks Bob, It was very detailed. But I guess the tiny bit that was missing in my code was the escaped pipe using the caret character. But this would be helpful for future reference. :) –  Nassign Mar 28 '12 at 9:29
add comment

You may want to check PsTools by Microsoft (Sysinternals). There is an tool called PsService where you can remotely check the services via the parameter \\computer

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.