Announcement

Collapse
No announcement yet.

Stop services script hangs

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

  • Stop services script hangs

    I've written a script that is supposed to stop all the services it can and then reboot the server. I tested it and it seemed to work. I schedule it to run once a week and it ran the first week.

    I find today that my script didn't work this past Sunday. I open scheduled tasks and notice it's still in the running state.

    I know that sometimes a user is left logged on so I was wondering if perhaps it had something to do with needing to be logged in to run properly. Here's the script:
    Code:
    On Error Resume Next
    
    Set Arg = Wscript.Arguments
    
    If NOT arg(0) = "/r" Then
      Wscript.Quit
    End If
    
    strComputer = "."
    
    Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
    
    objSWbemLocator.Security_.privileges.addasstring "sedebugprivilege", true 
    
    Set objWMIService = objSWbemLocator.ConnectServer(strComputer, _ 
        "root\CIMV2") 
    
    Set colService = objWMIService.ExecQuery("SELECT * FROM win32_Service")
    
    
    For Each objService in colService
      objService.StopService()
    Next
    
    set objShell = CreateObject("WScript.Shell") 
    objShell.Run "shutdown -r -t 0 -f"
    Any input would be appreciated.
    Regards,
    Jeremy

    Network Consultant/Engineer
    Baltimore - Washington area and beyond
    www.gma-cpa.com

  • #2
    Re: Stop services script hangs

    It is probably waiting for a user action,
    you will not see an error because the scheduled task is executed with a non interactive RunAs service.

    I am not sure at what point in the process the task hangs,
    Can you find out if none, some or al ready all services stopped at that point?


    You can try the following changes in the code,
    1. this WQL:
    Code:
    Set colService = objWMIService.ExecQuery("SELECT * FROM " _
       & "win32_Service Where (Started = True And AcceptStop = True) And " _
       & "(Name <> 'winmgmt' And Name <> 'seclogon' And Name <> 'schedule')")
    2. Use the StopService Method like this,
    Code:
    intReturn = objService.StopService()
    3. Add a, approx. 15-20 sec, delay before reboot;
    Code:
    On Error GoTo 0
    set objShell = CreateObject("WScript.Shell")
    
    ' the next popup won't be visible, but it will provide a beeb and a delay of 20 seconds
    intReturn = objShell.popup("a planned system reboot is in progress" ,_
       20, "WAIT...", 256+48+4096)
    If Not intReturn = -1 then wscript.sleep 10000
    
    intReturn = objShell.Run("""%SystemRoot%\system32\shutdown.exe"" -r -t 0 -f",,false)
    wscript.quit

    \Rems
    Last edited by Rems; 14th May 2008, 19:22.

    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


    • #3
      Re: Stop services script hangs

      Thanks Rems. I'll add the code tomorrow. I think I can test it on Thursday... if I have time.

      When I saw that the task was still running, IIRC, I checked the services and all the Automatic ones (except for a few like Perfmon which I expect to be stopped) were running.
      Regards,
      Jeremy

      Network Consultant/Engineer
      Baltimore - Washington area and beyond
      www.gma-cpa.com

      Comment


      • #4
        Re: Stop services script hangs

        It's not a good idea to try to stop all services before rebooting. Stop Workstation, Netlogon and DHCP Client services and you will see that it might take ages for the server to reboot after that.
        Guy Teverovsky
        "Smith & Wesson - the original point and click interface"

        Comment


        • #5
          Re: Stop services script hangs

          Guy is right, de computer does have a slow logon after you terminated these services.

          It is beter to stop just the service(s) of an certain application(s) if nesserary or, if can help to speed up the shutdown/reboot.
          Other services will be stopped automatically and started when rebooting the system anyway.

          \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: Stop services script hangs

            Originally posted by Rems View Post
            de computer does have a slow logon after you terminated these services.
            Actually when I tested it the reboot was relatively quick.

            But I'll add exceptions for those services you mentioned Guy. Is there any other services I should add to the exception list?

            The reason for stopping the services is because the computer would hang during the shutdown. I did want to go through and test to see which service was causing the problem but it is very hard to get in to this client after hours to eliminate the services one by one.
            Regards,
            Jeremy

            Network Consultant/Engineer
            Baltimore - Washington area and beyond
            www.gma-cpa.com

            Comment


            • #7
              Re: Stop services script hangs

              I do not have a definitive list, but I'd also thow in "Security Accounts Manager" and RPC services into the list.
              Guy Teverovsky
              "Smith & Wesson - the original point and click interface"

              Comment


              • #8
                Re: Stop services script hangs

                Originally posted by JeremyW View Post
                I've written a script that is supposed to stop all the services it can and then reboot the server. I tested it and it seemed to work. I schedule it to run once a week and it ran the first week.

                I find today that my script didn't work this past Sunday. I open scheduled tasks and notice it's still in the running state.

                I know that sometimes a user is left logged on so I was wondering if perhaps it had something to do with needing to be logged in to run properly. Here's the script:
                Code:
                On Error Resume Next
                
                Set Arg = Wscript.Arguments
                
                If NOT arg(0) = "/r" Then
                  Wscript.Quit
                End If
                
                strComputer = "."
                
                Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
                
                objSWbemLocator.Security_.privileges.addasstring "sedebugprivilege", true 
                
                Set objWMIService = objSWbemLocator.ConnectServer(strComputer, _ 
                    "root\CIMV2") 
                
                Set colService = objWMIService.ExecQuery("SELECT * FROM win32_Service")
                
                
                For Each objService in colService
                  objService.StopService()
                Next
                
                set objShell = CreateObject("WScript.Shell") 
                objShell.Run "shutdown -r -t 0 -f"
                Any input would be appreciated.
                An very very bad practice, this way the service's will not handle the OnShutDown command when the server reboot's therefore some vital resources etc can be left unhandeld before the server reboot's

                Unless you are absolutly sure that none off youre service's depend on the onshutdown command you should NOT do this.

                Furthermore, if an programmer failed to set dependency on an service this could also mean an service could 'hang'

                Dependency meaning for example that Service X uses the eventlog to write it's message's but if the programmer does not specify that his service depends on the evntlogservice running than the eventlogservice could be stopped before the dependent service X is stopped, resulting that Service X would probably hang if this was not handled correctly when it tries to write to the eventlog.
                Last edited by RonaldM; 24th May 2008, 08:12.

                Comment


                • #9
                  Re: Stop services script hangs

                  Originally posted by JeremyW View Post
                  Actually when I tested it the reboot was relatively quick.

                  But I'll add exceptions for those services you mentioned Guy. Is there any other services I should add to the exception list?

                  The reason for stopping the services is because the computer would hang during the shutdown. I did want to go through and test to see which service was causing the problem but it is very hard to get in to this client after hours to eliminate the services one by one.
                  Sounds to me like the 'hanging' service has not set all off it's dependency's set correctly i.o.w. The service probably depends on an service te be running but that service was allready stopped before the hanging service was stopped.

                  Check out the service's that have NO dependy what so ever, normally almost (99,9%) off every 'custom ' service (i.o.w. not windows default service's) depends on the Eventlog service to be running.

                  If the hanging service should depend on user action then you should check all the service's that have the "Allow service to interact with the desktop" checkbox checked on the logon tab off the service property's

                  That should limit youre search as Service's which do not have this box checked cannot interact with user's and therefore cannot be waiting for user input.

                  Comment


                  • #10
                    Re: Stop services script hangs

                    Rems, Guy, and RonaldM, thanks so much for the replies. You've been very helpful in getting a safer, more effective script.


                    Now for the embarrassing part .....
                    1) the script that was just continuously running was because I pointed the task to the wrong script. I had created two versions of the script. One for when you want to interactively restart the server which prompted you to confirm the shutdown and another for the scheduled task which required an argument so you couldn't accidentally reboot the server. I had pointed it to the one that asked for confirmation.

                    2) The other server would run the task but nothing would happen. Well I finally got in there after hours and was able to see that the task was not passing the argument.
                    In the path for the task I was just putting the path to the script. When you do that with VBS it doesn't pass the arguments. So I put in cscript //nologo <script path> /r and that worked nicely in my test.
                    Regards,
                    Jeremy

                    Network Consultant/Engineer
                    Baltimore - Washington area and beyond
                    www.gma-cpa.com

                    Comment

                    Working...
                    X