Announcement

Collapse
No announcement yet.

Remotely Restarting a Service Reliably

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Remotely Restarting a Service Reliably

    I need to be able to reliably and remotely restart a service from cmd/batch.

    So far I have been doing
    Code:
    sc \\server stop servicename
    sc \\server start servicename
    But because of a timing issue this invariably leaves the service as just stopped. So I introduced a time wait between the two commands, but this doesn't always work, if the server is slow to respond to the first one, it's not ready by the time of the second one and the service never restarts.

    Anyone got any good reliable method for restarting a service and making sure it comes back up from cmd/batch?

  • #2
    Re: Remotely Restarting a Service Reliably

    There no 100% reliable way to do it.

    This is how I do it.

    Code:
    psservice /? /accepteula
    psservice \\server -u administrator -p password stop service
    type NUL | choice /N /CY /TY,10 >NUL
    psservice \\server -u administrator -p password start service
    This causes there to be a 10 second delay before it tries to start the service again.
    You can do the same thing using SC. I used psservice here because it was across a VPN to one of our clients in a domain where I had no credentials.
    Last edited by ahinson; 20th December 2007, 16:01.
    Andrew

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

    Comment


    • #3
      Re: Remotely Restarting a Service Reliably

      I know pstools but I want to do this with standard tools, not add ons since it's for a batch that I want to work from anywhere and I can't assume to have pstools.

      This is why I use sc, it's always there by default.

      Comment


      • #4
        Re: Remotely Restarting a Service Reliably

        Originally posted by humbletech99 View Post
        I know pstools but I want to do this with standard tools, not add ons since it's for a batch that I want to work from anywhere and I can't assume to have pstools.

        This is why I use sc, it's always there by default.
        Well ignore the tool used. This is the important line...

        Code:
        type NUL | choice /N /CY /TY,10 >NUL
        choice.com is included with Windows and that's what allows the 10 second delay between running the next line in the batch file.
        Andrew

        ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

        Comment


        • #5
          Re: Remotely Restarting a Service Reliably

          I was playing with this code (DISCOde )
          Code:
          @echo off
          
          (Set service=AVSynMgr)
          (Set strComputer=Workstation065)
          
          Sc.exe \\%strComputer% stop "%service%" >Nul&&REM
          
          :StopLoop  -------------------------------------------------------
          title STOPPING SERVICE & COLOR 9E
          For /F  "tokens=3 delims=: " %%i In ('sc.exe \\%strComputer% query "%service%" ^|Findstr /i "state" ^|Findstr /i ":" ') Do (
              Set State=%%i)
          echo.%state% |Findstr/i "PENDING"&&(Goto:StopLoop)
          
          If %state%==STOPPED (
          Sc.exe \\%strComputer% start "%service%" >Nul&&REM
           )
          
          :StartLoop  -------------------------------------------------------
          title STARTING SERVICE & COLOR 20
          For /F  "tokens=3 delims=: " %%i In ('sc.exe \\%strComputer% query "%service%" ^|Findstr /i "state" ^|Findstr /i ":" ') Do (
              Set State=%%i)
          echo.%state% |Findstr/i "PENDING"&&(Goto:StartLoop)
          \Rems

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment


          • #6
            Re: Remotely Restarting a Service Reliably

            The most reliable way is thru WMI because WMI get's an reply from the remote computer about the requested action.


            Stop Service example generated by : WMI code creator from

            http://www.microsoft.com/downloads/d...displaylang=en

            Code:
            strComputer = "FullComputerName" 
            strDomain = "DOMAIN" 
            Wscript.StdOut.Write "Please enter your user name:"
            strUser = Wscript.StdIn.ReadLine 
            Set objPassword = CreateObject("ScriptPW.Password")
            Wscript.StdOut.Write "Please enter your password:"
            strPassword = objPassword.GetPassword()
            Wscript.Echo
            
            Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") 
            Set objWMIService = objSWbemLocator.ConnectServer(strComputer, _ 
                "root\CIMV2", _ 
                strUser, _ 
                strPassword, _ 
                "MS_409", _ 
                "ntlmdomain:" + strDomain) 
            ' Obtain an instance of the the class 
            ' using a key property value.
            Set objShare = objWMIService.Get("Win32_Service.Name='Alerter'")
            
            ' no InParameters to define
            
            ' Execute the method and obtain the return status.
            ' The OutParameters object in objOutParams
            ' is created by the provider.
            Set objOutParams = objWMIService.ExecMethod("Win32_Service.Name='Alerter'", "StopService")
            
            ' List OutParams
            Wscript.Echo "Out Parameters: "
            Wscript.echo "ReturnValue: " & objOutParams.ReturnValue

            Comment


            • #7
              Re: Remotely Restarting a Service Reliably

              thank you so much, I actually do have app but didn't get into it, now I've taken a double take on it, it's actually really really good... and I've also noticed a better non echoed way of getting a password input as well (not to hijack my own thread but I'm actually also looking for a way of doing this graphically and at the moment I am aiming at using an hta to implement the password box pop up)

              EDIT:
              Rems: that's actually quite nice...

              I have taken a simpler approach whereby I issue a stop, wait 2 secs, then issue a forced kill of the process, then wait 1 sec, then issue a start...

              it will never hang the way the above code might. If the service gets stuck, it will not restart, and hence this could be quite frustrating if you were cycling through a bunch of servers only to have one hang and the rest never arrive.
              Last edited by humbletech99; 2nd January 2008, 11:44.

              Comment

              Working...
              X