Simplest way to restart service on a remote computer

后端 未结 11 1927
我寻月下人不归
我寻月下人不归 2020-12-23 13:07

What\'s the easiest programmatic way to restart a service on a remote Windows system? Language or method doesn\'t matter as long as it doesn\'t require human interaction.

相关标签:
11条回答
  • 2020-12-23 13:53

    look at sysinternals for a variety of tools to help you achieve that goal. psService for example would restart a service on a remote machine.

    0 讨论(0)
  • 2020-12-23 13:59

    This is what I do when I have restart a service remotely with different account

    Open a CMD with different login

    runas /noprofile /user:DOMAIN\USERNAME cmd
    

    Use SC to stop and start

    sc \\SERVERNAME query Tomcat8
    sc \\SERVERNAME stop Tomcat8
    sc \\SERVERNAME start Tomcat8
    
    0 讨论(0)
  • 2020-12-23 14:04

    There will be so many instances where the service will go in to "stop pending".The Operating system will complain that it was "Not able to stop the service xyz." In case you want to make absolutely sure the service is restarted you should kill the process instead. You can do that by doing the following in a bat file

    taskkill /F /IM processname.exe
    timeout 20
    sc start servicename
    

    To find out which process is associated with your service, go to task manager--> Services tab-->Right Click on your Service--> Go to process.

    Note that this should be a work around until you figure out why your service had to be restarted in the first place. You should look for memory leaks, infinite loops and other such conditions for your service to have become unresponsive.

    0 讨论(0)
  • 2020-12-23 14:06

    I believe PowerShell now trumps the "sc" command in terms of simplicity:

    Restart-Service "servicename"
    
    0 讨论(0)
  • 2020-12-23 14:07

    I recommend the method given by doofledorfer.

    If you really want to do it via a direct API call, then look at the OpenSCManager function. Below are sample functions to take a machine name and service, and stop or start them.

    function ServiceStart(sMachine, sService : string) : boolean;  //start service, return TRUE if successful
    var schm, schs : SC_Handle;
        ss         : TServiceStatus;
        psTemp     : PChar;
        dwChkP     : DWord;
    begin
      ss.dwCurrentState := 0;
      schm := OpenSCManager(PChar(sMachine),Nil,SC_MANAGER_CONNECT);  //connect to the service control manager
    
      if(schm > 0)then begin // if successful...
        schs := OpenService( schm,PChar(sService),SERVICE_START or SERVICE_QUERY_STATUS);    // open service handle, start and query status
        if(schs > 0)then begin     // if successful...
          psTemp := nil;
          if (StartService(schs,0,psTemp)) and (QueryServiceStatus(schs,ss)) then
            while(SERVICE_RUNNING <> ss.dwCurrentState)do begin
              dwChkP := ss.dwCheckPoint;  //dwCheckPoint contains a value incremented periodically to report progress of a long operation.  Store it.
              Sleep(ss.dwWaitHint);  //Sleep for recommended time before checking status again
              if(not QueryServiceStatus(schs,ss))then
                break;  //couldn't check status
              if(ss.dwCheckPoint < dwChkP)then
                Break;  //if QueryServiceStatus didn't work for some reason, avoid infinite loop
            end;  //while not running
          CloseServiceHandle(schs);
        end;  //if able to get service handle
        CloseServiceHandle(schm);
      end;  //if able to get svc mgr handle
      Result := SERVICE_RUNNING = ss.dwCurrentState;  //if we were able to start it, return true
    end;
    
    function ServiceStop(sMachine, sService : string) : boolean;  //stop service, return TRUE if successful
    var schm, schs : SC_Handle;
        ss         : TServiceStatus;
        dwChkP     : DWord;
    begin
      schm := OpenSCManager(PChar(sMachine),nil,SC_MANAGER_CONNECT);
    
      if(schm > 0)then begin
        schs := OpenService(schm,PChar(sService),SERVICE_STOP or SERVICE_QUERY_STATUS);
        if(schs > 0)then begin
          if (ControlService(schs,SERVICE_CONTROL_STOP,ss)) and (QueryServiceStatus(schs,ss)) then
            while(SERVICE_STOPPED <> ss.dwCurrentState) do begin
              dwChkP := ss.dwCheckPoint;
              Sleep(ss.dwWaitHint);
              if(not QueryServiceStatus(schs,ss))then
                Break;
    
              if(ss.dwCheckPoint < dwChkP)then
                Break;
            end;  //while
          CloseServiceHandle(schs);
        end;  //if able to get svc handle
        CloseServiceHandle(schm);
      end;  //if able to get svc mgr handle
      Result := SERVICE_STOPPED = ss.dwCurrentState;
    end;
    
    0 讨论(0)
提交回复
热议问题