通过UI和批处理文件停止Windows服务(Stopping Windows Services via UI vs batch file)

编程入门 行业动态 更新时间:2024-10-26 17:20:45
通过UI和批处理文件停止Windows服务(Stopping Windows Services via UI vs batch file)

我试图在批处理文件中停止服务时遇到一个奇怪的问题,我想知道是否有办法获得有关问题的更详细信息,或者是否有人知道它可能发生的原因。 首先,它在Windows Server 2008 R2 Standard上运行。

我们有一个Windows服务偶尔需要停止然后重新启动,同时执行更新。 我们一直在通过用户界面进行此操作,用户可以点击该服务,然后使用按钮“停止”它。 这已经完成了数百次,服务从未停止或成功启动。

为了实现自动化,我创建了一个批处理文件,然后我可以根据需要安排运行。 在测试时,我发现大约有3次中有3次,当发出“net stop”命令时,它会返回一条消息,说服务无法停止。

由于“网络停止”命令没有选项,我不确定我还能做什么。 更奇怪的是,当这个消息发生时,如果我然后转到UI,服务显示为不运行,就像停止成功一样。 此外,如果我然后发出“net start”命令,或点击UI中的“开始”按钮,该服务表示它已成功启动,并且所有内容似乎都正常运行。

因此,就好像这个关于未能停止服务的错误消息不正确而且服务确实停止而没有错误。 我认为这可能是服务需要更长时间才能阻止net命令愿意等待的问题。 但由于“网络停止”命令没有选项,似乎没有办法让它等待更长时间。 我可以忽略它,因为一切都按预期工作,但令我困扰的是这条消息正在返回。

我的第一个问题是,是否有一个更详细的错误日志与命令窗口中显示的消息相关联,我可以看到它认为究竟失败了什么? 第二,有其他任何人见过这种情况,要么有建议,要么修复它,要么我应该忽略它。

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.

最满意答案

这听起来像是设计行为。

如果您无法立即停止服务,请尝试以下方法:

:loop sc query myservice | findstr STOPPED && goto :stopped net stop myservice if %ERRORLEVEL% NEQ 0 goto :loop :stopped

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

更多推荐

本文发布于:2023-08-07 22:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466030.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:批处理文件   Windows   UI   Stopping   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!