I want to restart Windows service using command prompt in [Icons]
section using Inno Setup. Please help me to solve the problem.
问题:
回答1:
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.
回答2:
You could create a .bat-file with following content:
net stop "my service name"
net start "my service name"
回答3:
net.exe stop "servicename" && net.exe start "servicename"
回答4:
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.
回答5:
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));
回答6:
You can start and stop and query services using the SC command. As for innosetup i'm not sure.