How to restart service using command prompt?

2019-02-04 04:30发布

I want to restart Windows service using command prompt in [Icons] section using Inno Setup. Please help me to solve the problem.

6条回答
淡お忘
2楼-- · 2019-02-04 05:09

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));
查看更多
混吃等死
3楼-- · 2019-02-04 05:12

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

查看更多
Fickle 薄情
4楼-- · 2019-02-04 05:19

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

net stop "my service name"
net start "my service name"
查看更多
仙女界的扛把子
5楼-- · 2019-02-04 05:20
net.exe stop "servicename" && net.exe start "servicename"
查看更多
Summer. ? 凉城
6楼-- · 2019-02-04 05:22

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.

查看更多
老娘就宠你
7楼-- · 2019-02-04 05:30

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.

查看更多
登录 后发表回答