2013-03-03 92 views
0

我已经完成了停止服务,删除文件,安装文件和启动服务的批处理文件。直到现在它没有任何麻烦。但今天它没有安装这些文件。如何检查服务是否停止

经过一番搜索,我看到服务正在停止,并没有停止。 我正在使用网络停止“服务名称”停止服务。

如何检查服务何时停止或等待它们完全停止?

+0

查看答案http://stackoverflow.com/questions/133883/stop-and-start-a-service-via-batch-or-cmd-file或[如何检查最后一个命令的退出码批处理文件] [1]。 [1]:http://superuser.com/questions/194662/how-to-check-the-exit-code-of-the-last-command-in-batch-file – Mogsdad 2013-03-03 19:20:26

回答

1

wmic service where name="Service-Name" get state会告诉你服务是否正在运行或停止。您可以循环回到该命令并使用ping来暂停。

:running 
ping /n 2 0.0.0.0 >NUL 
for /f "tokens=2 delims=^=" %%I in ('wmic service where name^="Service-Name" get state /format:list') do (
    if #%%I==#Running goto running 
) 
rem When the script reaches this line, the service status is not "Running." 

或者如果你喜欢使用sc确定一个服务的状态:

:running 
ping /n 2 0.0.0.0 >NUL 
for /f "tokens=4" %%I in ('sc query Service-Name ^| find /i "state"') do (
    if #%%I==#RUNNING goto running 
) 
rem When the script reaches this line, the service status is not "Running." 

我不能肯定,但它可能是一个服务可以说“停”时,它实际上是“停止“或”运行“时,它实际上是”开始“。如果是这种情况,那么最好检查进程列表中是否存在进程。这也可以使用wmic:wmic process where name="appname.exe" get name或类似的方法来完成。

+0

这个代码:sc查询服务名称^ |找到/我“状态”是非常有帮助的。非常感谢你。 – elad 2013-03-05 08:08:35

相关问题