0

I am having a strange problem attempting to stop a service in a batch file and I'm wondering if there is a way to get more detailed information on the problem, or if someone knows why it might be occurring. First, this is running on Windows Server 2008 R2 Standard.

We have a Windows Service that occasionally needs to be stopped and then restarted, while updates are performed. We have been doing this through the UI, where a person would click on the service and then "Stop" it with the button. This has been done hundreds of times and the service has never failed to stop, or start successfully.

In an attempt to automate this, I created a batch file that I could then schedule to run as needed. When testing this, I have found that roughly 1 out of 3 times, when the "net stop" command is issued, it returns a message that the service could not be stopped.

As the "net stop" command has no options, I am not sure what else I could do. The stranger thing is, when this message happens, if I then go to the UI, the service appears as not running, as if the stop was successful. Furthermore, if I then either issue a "net start" command, or click on the Start button in the UI, the service says it starts successfully and everything appears to functioning as normal.

So, it's as if this error message about failing to stop the service is not correct and the service did in fact stop without error. I think it might be a matter of the service taking longer to stop that the net command is willing to wait. But as the "net stop" command has no options, there doesn't appear to be a way to have it wait longer. I could just ignore it, since everything is working as expected, but it bothers me that this message is being returned.

My first question would be, is there a more detailed error log somewhere associated with the message that is displayed in the command window that I could see what exactly it thinks failed? And second, has anyone else ever seen this happen, and either have a recommendation, either to fix it or that I should just ignore it.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
BlakeK
  • 11
  • 1
  • How long does "net stop" take to run? – Harry Johnston Jun 30 '15 at 20:27
  • I'd say the DOS command says "Stopping..." for roughly 30 seconds before coming back with the message that it failed to stop. Via the UI, successfully stopping the service probably takes between 60-90 seconds. – BlakeK Jul 01 '15 at 13:49

2 Answers2

0

That sounds like by-design behaviour.

If you can't make the service stop more promptly, try something like this:

:loop
sc query myservice | findstr STOPPED && goto :stopped
net stop myservice
if %ERRORLEVEL% NEQ 0 goto :loop
:stopped
Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
0

I just wanted to document for anyone who finds this in the future, I finally found a great solution at the following URL. A user provided very robust scripts for stopping a service, one for starting a service, and a third for cycling a service. Check them out here: Stopping/Starting a remote Windows service and waiting for it to open/close

Community
  • 1
  • 1
BlakeK
  • 11
  • 1