问题描述
我已经完成了一个批处理文件,该文件可以停止服务,删除文件,安装文件并启动服务.到现在为止还没有任何麻烦.但是今天它无法安装文件.
I have done a batch file that stop the services, delete files, install files, and start the services.till now it didn't have any trouble. but today it failed to install the files.
稍作搜索后,我发现服务正在停止而不是停止.我正在使用 net stop服务名称" 来停止服务.
After a little search I have seen that the services are on stopping and not on stopped.I am using net stop "Service-Name" to stop the services.
如何检查服务何时停止或等待它们完全停止?
How can I check when the services are stopped or wait for them to completely stop?
推荐答案
wmic service where name="Service-Name" get state
将向您显示服务正在运行还是已停止.您可以循环回到该命令并使用ping暂停.
wmic service where name="Service-Name" get state
will show you whether a service is running or stopped. You can loop back to that command and use a ping to pause.
: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
来确定服务的状态,则:
Or if you prefer using sc
to determine the state of a service:
: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
或类似的软件来完成.
I'm not certain, but it's possible that a service could say "stopped" when it's actually "stopping", or "running" when it's actually "starting." If that's the case, then you might be better off checking whether a process exists in the process list. That can also be done using wmic: wmic process where name="appname.exe" get name
or similar.
这篇关于如何检查服务是否停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!