如何使用命令提示符重新启动服务?

发布于 2024-12-11 02:39:06 字数 80 浏览 0 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

孤星 2024-12-18 02:39:06

您可以使用 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 and sc stop [service] to stop it. With some services net 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 use net stop [service] followed by net start [service]. This will be executed synchronously.

偏闹i 2024-12-18 02:39:06

您可以创建一个包含以下内容的 .bat 文件:

net stop "my service name"
net start "my service name"

You could create a .bat-file with following content:

net stop "my service name"
net start "my service name"
潇烟暮雨 2024-12-18 02:39:06
net.exe stop "servicename" && net.exe start "servicename"
net.exe stop "servicename" && net.exe start "servicename"
只为一人 2024-12-18 02:39:06

重新启动正在运行的服务:

net stop "service name" && net start "service name"

但是,如果您一开始不知道该服务是否正在运行并且想要重新启动或启动它,请使用以下命令:

net stop "service name" & net start "service name"

无论服务是否已在运行,这都有效。

作为参考,以下是有关条件处理符号的文档

To restart a running service:

net stop "service name" && net start "service name"

However, if you don't know if the service is running in the first place and want to restart or start it, use this:

net stop "service name" & net start "service name"

This works if the service is already running or not.

For reference, here is the documentation on conditional processing symbols.

玩套路吗 2024-12-18 02:39:06

这是我的代码,使用 SC 命令启动/停止 Windows 服务。如果服务无法启动/停止,它将打印日志信息。您可以通过 Inno Setup 尝试一下。

{ start a service }
Exec(ExpandConstant('{cmd}'), '/C sc start ServiceName', '',
     SW_HIDE, ewWaitUntilTerminated, ResultCode);
Log('sc start ServiceName:'+SysErrorMessage(ResultCode));
{ stop a service }
Exec(ExpandConstant('{cmd}'), '/C sc stop ServiceName', '',
     SW_HIDE, ewWaitUntilTerminated, ResultCode);
Log('sc stop ServiceName:'+SysErrorMessage(ResultCode));

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.

{ start a service }
Exec(ExpandConstant('{cmd}'), '/C sc start ServiceName', '',
     SW_HIDE, ewWaitUntilTerminated, ResultCode);
Log('sc start ServiceName:'+SysErrorMessage(ResultCode));
{ stop a service }
Exec(ExpandConstant('{cmd}'), '/C sc stop ServiceName', '',
     SW_HIDE, ewWaitUntilTerminated, ResultCode);
Log('sc stop ServiceName:'+SysErrorMessage(ResultCode));
余厌 2024-12-18 02:39:06

您可以使用 SC 命令启动、停止和查询服务。至于 innosetup 我不确定。

You can start and stop and query services using the SC command. As for innosetup i'm not sure.

满地尘埃落定 2024-12-18 02:39:06

PowerShell 具有 Restart-Service cmdlet,根据需要启动或重新启动服务。

Restart-Service cmdlet 向指定服务的 Windows 服务控制器发送一条停止消息,然后发送一条启动消息。如果服务已停止,则会启动该服务而不通知您错误。

您可以通过服务名称或显示名称来指定服务,也可以使用 InputObject 参数传递一个代表您要重新启动的每个服务的对象。

它比运行两个单独的命令更简单一些。

使用它的最简单方法只需直接传递服务名称或显示名称:

Restart-Service "$ServiceName"

它可以直接从标准 cmd 提示符使用如下命令:

powershell -Command "Restart-Service '%ServiceName%'"

PowerShell features a Restart-Service cmdlet, which either starts or restarts the service as appropriate.

The Restart-Service cmdlet sends a stop message and then a start message to the Windows Service Controller for a specified service. If a service was already stopped, it is started without notifying you of an error.

You can specify the services by their service names or display names, or you can use the InputObject parameter to pass an object that represents each service that you want to restart.

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:

Restart-Service "$ServiceName"

It can be used directly from the standard cmd prompt with a command like:

powershell -Command "Restart-Service '%ServiceName%'"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文