如何使用命令提示符重新启动服务?
我想使用 Inno Setup 在 [Icons]
部分中使用命令提示符重新启动 Windows 服务。请帮我解决问题。
I want to restart Windows service using command prompt in [Icons]
section using Inno Setup. Please help me to solve the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用
sc start [service]
启动服务,并使用sc stop [service]
停止它。对于某些服务net start [service]
也会做同样的事情。但如果您想在同一批次中使用它,请注意
sc stop
不会等待服务停止。在这种情况下,您必须使用net stop [service]
,然后使用net start [service]
。这将同步执行。You can use
sc start [service]
to start a service andsc stop [service]
to stop it. With some servicesnet start [service]
is doing the same.But if you want to use it in the same batch, be aware that
sc stop
won't wait for the service to be stopped. In this case you have to usenet stop [service]
followed bynet start [service]
. This will be executed synchronously.您可以创建一个包含以下内容的 .bat 文件:
You could create a .bat-file with following content:
重新启动正在运行的服务:
但是,如果您一开始不知道该服务是否正在运行并且想要重新启动或启动它,请使用以下命令:
无论服务是否已在运行,这都有效。
作为参考,以下是有关条件处理符号的文档。
To restart a running service:
However, if you don't know if the service is running in the first place and want to restart or start it, use this:
This works if the service is already running or not.
For reference, here is the documentation on conditional processing symbols.
这是我的代码,使用 SC 命令启动/停止 Windows 服务。如果服务无法启动/停止,它将打印日志信息。您可以通过 Inno Setup 尝试一下。
This is my code, to start/stop a Windows service using
SC
command. If the service fails to start/stop, it will print a log info. You can try it by Inno Setup.您可以使用 SC 命令启动、停止和查询服务。至于 innosetup 我不确定。
You can start and stop and query services using the SC command. As for innosetup i'm not sure.
PowerShell 具有
Restart-Service
cmdlet,根据需要启动或重新启动服务。它比运行两个单独的命令更简单一些。
使用它的最简单方法只需直接传递服务名称或显示名称:
它可以直接从标准 cmd 提示符使用如下命令:
PowerShell features a
Restart-Service
cmdlet, which either starts or restarts the service as appropriate.It is a little more foolproof than running two separate commands.
The easiest way to use it just pass either the service name or the display name directly:
It can be used directly from the standard cmd prompt with a command like: