Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 854885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:59:43+00:00 2026-05-15T07:59:43+00:00

The top answer to this question tells me how to stop/start a remote service.

  • 0

The top answer to this question tells me how to stop/start a remote service. Great.
Now, all I need is to wait for the actual stop/start to complete. So, what I’m looking for is a dos command to:

  1. Start a service, should return only after the service is started (or after a timeout, raising error level)
  2. Stop a service, return only after the service is stopped
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:59:44+00:00Added an answer on May 15, 2026 at 7:59 am

    I created a set of batch scripts that use sc.exe to do just this. They are attached below. To run these scripts, you should be a user with administration rights on the target machine and running this from a computer that is a member of the same domain. It’s possible to set it up to be able to run from outside of the domain (like from a VPN) but there are a lot of layers of security to work through involving firewalls, DCOM and security credentials.

    One of these days, I’m going to figure out the PowerShell equivalent, which should be much easier.

    safeServiceStart.bat

    @echo off
    :: This script originally authored by Eric Falsken
    
    IF [%1]==[] GOTO usage
    IF [%2]==[] GOTO usage
    
    ping -n 1 %1 | FIND "TTL=" >NUL
    IF errorlevel 1 GOTO SystemOffline
    SC \\%1 query %2 | FIND "STATE" >NUL
    IF errorlevel 1 GOTO SystemOffline
    
    :ResolveInitialState
    SC \\%1 query %2 | FIND "STATE" | FIND "STOPPED" >NUL
    IF errorlevel 0 IF NOT errorlevel 1 GOTO StartService
    SC \\%1 query %2 | FIND "STATE" | FIND "RUNNING" >NUL
    IF errorlevel 0 IF NOT errorlevel 1 GOTO StartedService
    SC \\%1 query %2 | FIND "STATE" | FIND "PAUSED" >NUL
    IF errorlevel 0 IF NOT errorlevel 1 GOTO SystemOffline
    echo Service State is changing, waiting for service to resolve its state before making changes
    sc \\%1 query %2 | Find "STATE"
    timeout /t 2 /nobreak >NUL
    GOTO ResolveInitialState
    
    :StartService
    echo Starting %2 on \\%1
    sc \\%1 start %2 >NUL
    
    GOTO StartingService
    :StartingServiceDelay
    echo Waiting for %2 to start
    timeout /t 2 /nobreak >NUL
    :StartingService
    SC \\%1 query %2 | FIND "STATE" | FIND "RUNNING" >NUL
    IF errorlevel 1 GOTO StartingServiceDelay
    
    :StartedService
    echo %2 on \\%1 is started
    GOTO:eof
    
    :SystemOffline
    echo Server \\%1 is not accessible or is offline
    GOTO:eof
    
    :usage
    echo %0 [system name] [service name]
    echo Example: %0 server1 MyService
    echo.
    GOTO:eof
    

    safeServiceStop.bat

    @echo off
    :: This script originally authored by Eric Falsken
    
    IF [%1]==[] GOTO usage
    IF [%2]==[] GOTO usage
    
    ping -n 1 %1 | FIND "TTL=" >NUL
    IF errorlevel 1 GOTO SystemOffline
    SC \\%1 query %2 | FIND "STATE" >NUL
    IF errorlevel 1 GOTO SystemOffline
    
    :ResolveInitialState
    SC \\%1 query %2 | FIND "STATE" | FIND "RUNNING" >NUL
    IF errorlevel 0 IF NOT errorlevel 1 GOTO StopService
    SC \\%1 query %2 | FIND "STATE" | FIND "STOPPED" >NUL
    IF errorlevel 0 IF NOT errorlevel 1 GOTO StopedService
    SC \\%1 query %2 | FIND "STATE" | FIND "PAUSED" >NUL
    IF errorlevel 0 IF NOT errorlevel 1 GOTO SystemOffline
    echo Service State is changing, waiting for service to resolve its state before making changes
    sc \\%1 query %2 | Find "STATE"
    timeout /t 2 /nobreak >NUL
    GOTO ResolveInitialState
    
    :StopService
    echo Stopping %2 on \\%1
    sc \\%1 stop %2 %3 >NUL
    
    GOTO StopingService
    :StopingServiceDelay
    echo Waiting for %2 to stop
    timeout /t 2 /nobreak >NUL
    :StopingService
    SC \\%1 query %2 | FIND "STATE" | FIND "STOPPED" >NUL
    IF errorlevel 1 GOTO StopingServiceDelay
    
    :StopedService
    echo %2 on \\%1 is stopped
    GOTO:eof
    
    :SystemOffline
    echo Server \\%1 or service %2 is not accessible or is offline
    GOTO:eof
    
    :usage
    echo Will cause a remote service to STOP (if not already stopped).
    echo This script will waiting for the service to enter the stopped state if necessary.
    echo.
    echo %0 [system name] [service name] {reason}
    echo Example: %0 server1 MyService
    echo.
    echo For reason codes, run "sc stop"
    GOTO:eof
    

    safeServiceRestart.bat

    @echo off
    :: This script originally authored by Eric Falsken
    
    if [%1]==[] GOTO usage
    if [%2]==[] GOTO usage
    
    ping -n 1 %1 | FIND "TTL=" >NUL
    IF errorlevel 1 GOTO SystemOffline
    SC \\%1 query %2 | FIND "STATE" >NUL
    IF errorlevel 1 GOTO SystemOffline
    
    :ResolveInitialState
    SC \\%1 query %2 | FIND "STATE" | FIND "RUNNING" >NUL
    IF errorlevel 0 IF NOT errorlevel 1 GOTO StopService
    SC \\%1 query %2 | FIND "STATE" | FIND "STOPPED" >NUL
    IF errorlevel 0 IF NOT errorlevel 1 GOTO StartService
    SC \\%1 query %2 | FIND "STATE" | FIND "PAUSED" >NUL
    IF errorlevel 0 IF NOT errorlevel 1 GOTO SystemOffline
    echo Service State is changing, waiting for service to resolve its state before making changes
    sc \\%1 query %2 | Find "STATE"
    timeout /t 2 /nobreak >NUL
    GOTO ResolveInitialState
    
    :StopService
    echo Stopping %2 on \\%1
    sc \\%1 stop %2 %3 >NUL
    
    GOTO StopingService
    :StopingServiceDelay
    echo Waiting for %2 to stop
    timeout /t 2 /nobreak >NUL
    :StopingService
    SC \\%1 query %2 | FIND "STATE" | FIND "STOPPED" >NUL
    IF errorlevel 1 GOTO StopingServiceDelay
    
    :StopedService
    echo %2 on \\%1 is stopped
    GOTO StartService
    
    :StartService
    echo Starting %2 on \\%1
    sc \\%1 start %2 >NUL
    
    GOTO StartingService
    :StartingServiceDelay
    echo Waiting for %2 to start
    timeout /t 2 /nobreak >NUL
    :StartingService
    SC \\%1 query %2 | FIND "STATE" | FIND "RUNNING" >NUL
    IF errorlevel 1 GOTO StartingServiceDelay
    
    :StartedService
    echo %2 on \\%1 is started
    GOTO:eof
    
    :SystemOffline
    echo Server \\%1 or service %2 is not accessible or is offline
    GOTO:eof
    
    :usage
    echo Will restart a remote service, waiting for the service to stop/start (if necessary)
    echo.
    echo %0 [system name] [service name] {reason}
    echo Example: %0 server1 MyService
    echo.
    echo For reason codes, run "sc stop"
    GOTO:eof
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

See the top answer to this question: What exactly is Spring Framework for? Im
The top voted answer to this SA question ( Objective C Static Class Level
My question so general, but I think the answer will be specific. All I
I was trying to answer this question. As suggested by the accepted answer, the
I'm new to Quartz2d so I hope this is an easy question to answer.
In this answer to a Meta question , Jeff states that he has fixed
This question is an continuation on another StackOverflow post where no clear answer is
I was trying to find an answer for this question for a while but
This is a best practice question, and I expect the answer to be it
Aplogies for top posting. This has buggged me for years and now I really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.