25

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

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Nithin
  • 921
  • 3
  • 10
  • 17
  • Didn't I give [an answer to this](http://stackoverflow.com/questions/7803337/how-to-run-windows-start-service-using-inno-setup/7804288#7804288) in the comments when you asked last time? Use a small wrapper EXE to provide the manifest to give the required permissions and start/restart the service. – Deanna Oct 20 '11 at 14:47
  • This question is off-topic as it's asking 'how can I restart a service in command prompt', and is not specific to innosetup. – AStopher Aug 31 '15 at 09:06

7 Answers7

37

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.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Fischermaen
  • 12,238
  • 2
  • 39
  • 56
  • Using sc start + sc stop doesn't work for me (the start part doesn't work). However using net stop + net start does work! – Anthony Brien Dec 06 '13 at 21:42
  • 1
    Have you tried to use quotation marks for service name? – senyor Mar 05 '14 at 17:48
  • 5
    `sc` has the problem it doesn't wait for start/stop to finish, it only sends a start/stop request to a service - so you can't `st stop Foo && sc start Foo`, because it fails - it tries to start the service before it has finished stopping. `net` is the correct one to use here, because it waits for the start/stop to finish. – Jakub Januszkiewicz Jun 04 '14 at 06:05
  • how do we make sure that the service has stopped successfully before running the start command? The stop process might be in progress when we run the start command again. – Adil Malik Sep 10 '14 at 11:44
  • What if I don't want to run it as an admin, but know admin permissions? Is there a way to run a command with username and password to restart it? – Serob_b Dec 13 '21 at 21:00
23

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

net stop "my service name"
net start "my service name"
Seybsen
  • 14,989
  • 4
  • 40
  • 73
7
net.exe stop "servicename" && net.exe start "servicename"
oluies
  • 17,694
  • 14
  • 74
  • 117
6

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.

Jamie Cockburn
  • 7,379
  • 1
  • 24
  • 37
  • the second version with `||` is very incorrect - if the service is running, it will be stopped, the command succeeds and the second command is NOT executed at all. try `echo a || echo b` – Knaģis Dec 22 '16 at 08:24
2

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));
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
LEo
  • 442
  • 5
  • 21
1

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 'Service Name'

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

powershell -command "Restart-Service 'Service Name'"
Community
  • 1
  • 1
mwfearnley
  • 3,303
  • 2
  • 34
  • 35
1

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
rerun
  • 25,014
  • 6
  • 48
  • 78