I am trying to make sure the batch file knows the service "Tomcat7" is fully stopped before trying to start the "Tomcat7" service again. I know "NET STOP" and "NET START" are supposed to wait to return until each have fully stopped or started, but I have seen cases where it continues on anyways, possibly due to a long timeout wait sometimes.


net stop Tomcat7

rd /s /q "C:\storefront\live\work"
rd /s /q "C:\storefront\live\logs"

for /F "tokens=3 delims=: " %%H in ('sc query Tomcat7 ^| findstr "        STATE"') do (
  if /I "%%H" EQ "STOPPED" (
    net start Tomcat7
  • There is a typo in if /I "%%H" EQ "STOPPED" (. Should be EQU instead of EQ – JosefZ Jun 23 '15 at 20:58

Use sc query Tomcat7 to get the status of the service.

  • thanks for the input Steven. Do you think the for statement above looks good with your use of sc query? – Matt Weick Jun 23 '15 at 17:15
  • You may consider adding a short wait between the for loop iterations. Use the timeout command. – Steven Jun 23 '15 at 17:19
sc start stisvc || Echo Service Not Fully Exited

Doesn't this do what you want? Remember we try things and catch errors, not test and then do. Think about why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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