How to restart service using command prompt?

前端 未结 7 1732
一生所求
一生所求 2020-12-29 04:15

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

相关标签:
7条回答
  • 2020-12-29 04:57

    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.

    0 讨论(0)
  • 2020-12-29 05:11

    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));
    
    0 讨论(0)
  • 2020-12-29 05:12

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

    net stop "my service name"
    net start "my service name"
    
    0 讨论(0)
  • 2020-12-29 05:15

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

    0 讨论(0)
  • 2020-12-29 05:16

    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'"
    
    0 讨论(0)
  • 2020-12-29 05:18
    net.exe stop "servicename" && net.exe start "servicename"
    
    0 讨论(0)
提交回复
热议问题