AppWorx, Dollar Universe and Sysload Community

Expand all | Collapse all

[Solved] Windows service stop script - How to stop a windows service correctly

  • 1.  [Solved] Windows service stop script - How to stop a windows service correctly

    Posted Jun 17, 2008 06:12 AM
    How to stop a windows service correctly
    Hi

    I am trying to script a service stop in a uproc on windows using "net stop %service_name%" and the return code for the uproc is "set resexe=%errorlevel%"

    The problem is that the command ends correctly but the system log displays "service could not be stopped." And the service stops a minute later... So my other jobs are not started depend of this shutdown uproc cannot start

    Is their a way for my uproc to wait for the real end of the service ?


  • 2.  [Solved] Windows service stop script - How to stop a windows service correctly

    Posted Jun 17, 2008 06:21 AM
    Hi Bill,

    what is the exact Windows version being used? It it a physical box or a virtualized environment? Does the box seem under heavy load with poor response time when you try to stop the service?  What service are your tying to stop?

      Kenny


  • 3.  [Solved] Windows service stop script - How to stop a windows service correctly

    Posted Jun 17, 2008 09:02 AM
    [quote name='kenny' post='461' date='June 17 2008, 12:20 PM']what is the exact Windows version being used?[/quote]
    XP,W2000 and W2003 /rolleyes.gif' class='bbc_emoticon' alt=':)' />

    [quote]It it a physical box or a virtualized environment?[/quote] physical

    [quote]Does the box seem under heavy load with poor response time when you try to stop the service?[/quote] no, cpu usage is as usual

    [quote]What service are your tying to stop?[/quote] I don't know, It just takes time in the "stopping" status


    In any case, isn't this related to the usage of the net start command ? Do we have an alternative (the kind of tool that would be great on sysinternals.com)


  • 4.  [Solved] Windows service stop script - How to stop a windows service correctly

    Posted Jun 17, 2008 10:43 AM
    The thing is if the service you are trying to stop is linked to other services this may explain the "delay".

    I ran a quick test on my Windows XP box in a$U batch to stop a Windows Service for a Dollar Universe user (this kind of service is *not* linked to any other service):

    C:\WINDOWS\system32>net start
    Les services Windows suivants ont été lancés:
       ...
       Univer$e sup510a
       Univer$e sup510e
       ...
    La commande s'est terminée correctement.

    C:\WINDOWS\system32>net stop "Univer$e sup510e"
    Le service Univer$e sup510e s'arrête..
    Le service Univer$e sup510e a été arrêté.

    C:\WINDOWS\system32>echo "Errorlevel" 0
    "Errorlevel" 0

    C:\WINDOWS\system32>net start
    Les services Windows suivants ont été lancés:
       ...
       Univer$e sup510a
       ...
    La commande s'est terminée correctement.

    All works fine, the command net stop only gives back the control once the service is stopped.


  • 5.  [Solved] Windows service stop script - How to stop a windows service correctly

    Posted Jun 18, 2008 07:20 AM
    Yes but I have a service that is "starting" during a rather long time.

    If I immediately do the "net start" command in the uproc (or after getting the prompt back on cmd), my service is not listed but if I wait for a couple minutes it does!


  • 6.  [Solved] Windows service stop script - How to stop a windows service correctly

    Posted Jun 18, 2008 09:43 AM
    Hi Bill,

    errr... in the opening of the topic, you mentioned a "net stop" and now you've just mentioned "net start"!

    There again, a service may need other services to start before it can start so this may take a little time.

    Otherwise, when you restart a service you also have 2 messages that mention that the service is 1/ starting 2/started and only then the command gives back the control.

    Can you name a service for which you have the issue? And is it at start / stop / both?

    Can you post the script / job log or a screenshot if you have the same issue in a cmd window interactively.

    Cheers,

    Michel


  • 7.  [Solved] Windows service stop script - How to stop a windows service correctly

    Posted Jul 02, 2008 07:29 AM
    Hello Bill,

    I think the tips is to do a test after your "net Stop" to see if the service is still started or not:

    net stop your_service
    set resexe=%errorlevel%

    :LOOP
    net start > temp_file
    %UXEXE%\uxsleep 10
    FIND /I "your_service" temp_file
    if %ERRORLEVEL%==0 GOTO LOOP

    Hope it Helps

    ++
    Pierre


  • 8.  [Solved] Windows service stop script - How to stop a windows service correctly

    Posted Jul 02, 2008 10:57 AM
    [quote name='PCL' post='520' date='July 2 2008, 11:28 AM']Hello Bill,

    I think the tips is to do a test after your "net Stop" to see if the service is still started or not:

    net stop your_service
    set resexe=%errorlevel%

    :LOOP
    net start > temp_file
    %UXEXE%\uxsleep 10
    FIND /I "your_service" temp_file
    if %ERRORLEVEL%==0 GOTO LOOP

    Hope it Helps

    ++
    Pierre[/quote]
    unfortunately this kind of script won't be 100% accurate : in case of dependant services which the depedent service was not corretly stopped, you'll enter in an interactive mode with such message :
    "Do you want to continue ? [Y/N]"


  • 9.  [Solved] Windows service stop script - How to stop a windows service correctly

    Posted Jul 03, 2008 11:51 AM
    Hello.

    I think what you would like is doable, but would require some scripting.

    Please, check the Windows commande named "sc".

    There are many possibilities regarding this command. It controls the services on a extremly more powerful way than "net" can.

    A few examples :

    sc enumdepend will provide you a list of depencies on a specific service.
    [code]C:\Documents and Settings\glc>sc enumdepend INT533_frwpmglc_IO_X
    Enum: entriesRead  = 3

    SERVICE_NAME: INT533_frwpmglc_SUR_X
    DISPLAY_NAME: Univer$e INT533_frwpmglc_SUR_X
              TYPE                  : 10  WIN32_OWN_PROCESS
              STATE                 : 4  RUNNING
                                            (STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
              WIN32_EXIT_CODE     : 0  (0x0)
              SERVICE_EXIT_CODE  : 0  (0x0)
              CHECKPOINT           : 0x0
              WAIT_HINT            : 0x0

    SERVICE_NAME: INT533_frwpmglc_ORD_X
    DISPLAY_NAME: Univer$e INT533_frwpmglc_ORD_X
              TYPE                  : 10  WIN32_OWN_PROCESS
              STATE                 : 4  RUNNING
                                            (STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
              WIN32_EXIT_CODE     : 0  (0x0)
              SERVICE_EXIT_CODE  : 0  (0x0)
              CHECKPOINT           : 0x0
              WAIT_HINT            : 0x0

    SERVICE_NAME: INT533_frwpmglc_ORD_S
    DISPLAY_NAME: Univer$e INT533_frwpmglc_ORD_S
              TYPE                  : 10  WIN32_OWN_PROCESS
              STATE                 : 1  STOPPED
                                            (NOT_STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
              WIN32_EXIT_CODE     : 1077        (0x435)
              SERVICE_EXIT_CODE  : 0  (0x0)
              CHECKPOINT           : 0x0
              WAIT_HINT            : 0x0
    Enum: more data, need 2828 bytes[/code]
    Nothing hold you to build a script that will :
    -list those child process
    -shutdown those who are started (as you can see, we can supervize their status)
    - and shutdown the service.

    You can use the command :
    sc stop nameoftheservice
    to send a request for a stop.

    There are even more possibilities.

    Check sc /? in a MS-DOS box.


    Cheers,
    Guillaume.


  • 10.  [Solved] Windows service stop script - How to stop a windows service correctly

    Posted Jul 03, 2008 12:04 PM
    Just a little add :

    There are no interactive messages in the use of commande "sc", which is definatly interesting when launched from Dollar Universe.


  • 11.  [Solved] Windows service stop script - How to stop a windows service correctly

    Posted Sep 17, 2008 03:52 AM
    Hi ,
       I've writing some time ago a vbscript program to restart a service on a computer.
       In this vbscript i stop the service then wait for a number of times the supplied time and if this time is reached and the service isn't stopped yet the script stops with an error.
      ps. :  in the script there is also the start of the service woth the same logic as above (use the parts of the scripts as needed).
    For this code to run you need to install Windows Script Host Version 5.6.
    Code :
    [code]' ReStartService.vbs
    ' Sample script to Stop or Start a Service
    ' -------------------------------------------------------'

    Option Explicit

    Dim objWMIService, objItem, objService
    Dim colListOfServices, strComputer, strService, intSleep
    On Error Resume Next
    ' --------------------------------------------------------------------------
    If Wscript.Arguments.Count < 2 Then
      wscript.Echo "Arguments incorrect 2 parameters required. " & vbCrLf _
         & "Parameter 1 = ComputerName, Parameter 2 = ServiceName, Parameter 3 = Wait time in seconds (default 15 sec)"
      Wscript.Quit(8)
    end if
    ' ---- computer name
    strComputer = Wscript.Arguments(0)
    ' ---- NB strService is case sensitive.
    strService = "'" & Wscript.Arguments(1) & "'"
    ' ---- wait time
    If Wscript.Arguments.Count =3 Then
      intSleep = Cint(Wscript.Arguments(2))
    else
      intSleep = 15
    end if
    Wscript.echo " ComputerName       = " & strComputer
    Wscript.echo " Service             = " & strService
    Wscript.echo " Wait time in Sec  = " & intSleep
    intSleep = intSleep *  1000
    ' --------------------------------------------------------------------------
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    if err.number <> 0 then
      Wscript.Echo "Error connecting to computer : " & err.description
      wscript.quit(99)
    end if
    ' --------------------------------------------------------------------------
    getServiceData
    ' --------------------------------------------------------------------------
    For Each objService in colListOfServices
    '     Wscript.Echo "Before stop " & objService.DisplayName  & VbTab & objService.State
      if objService.State <> "Stopped" Then
         objService.StopService()
         if err.number <> 0 then
           Wscript.Echo "Error stopping service : " & err.description
           wscript.quit(99)
         end if
      end if
    Next
    ' --------------------------------------------------------------------------
    Dim ind, nbrRetry
    ind=1
    nbrRetry=0
    While ind=1
    '  Wscript.Echo "Waiting for 15000 milleseconds"
      WSCript.Sleep intSleep
      nbrRetry=nbrRetry+1
      getServiceData
      For Each objService in colListOfServices
         Wscript.Echo "Object state " & objService.State
         if objService.State = "Stopped" Then
           ind=0
         else
           if nbrRetry>5 Then
              Wscript.Echo "Error Service did not stop within reasonabel time ! "
              wscript.quit(99)
           end if
         end if
      Next
    Wend
    ' --------------------------------------------------------------------------
    For Each objService in colListOfServices
    '     Wscript.Echo "Before start " & objService.DisplayName  & VbTab & objService.State
         objService.StartService()
    Next
    ' --------------------------------------------------------------------------
    ind=1
    nbrRetry=0
    While ind=1
    '  Wscript.Echo "Waiting for 15000 milleseconds"
      WSCript.Sleep intSleep
      nbrRetry=nbrRetry+1
      getServiceData
      For Each objService in colListOfServices
         Wscript.Echo "Object state " & objService.State
         if objService.State = "Running" Then
           ind=0
         else
           if nbrRetry>5 Then
              Wscript.Echo "Error Service did not start within reasonabel time ! "
              wscript.quit(99)
           end if
         end if
      Next
    Wend
    ' --------------------------------------------------------------------------
    WScript.Echo "Your "& strService & " service has Restarted"

    WScript.Quit
    ' --------------------------------------------------------------------------
    '------------------------ get service data ---------------------------------
    Sub getServiceData()
      Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name =" & strService & " ")
      if err.number <> 0 then
         Wscript.Echo "Error getting service data : " & err.description
         wscript.quit(99)
      end if
    ' --------------------------------------------------------------------------
      If colListOfServices.Count = 0 then
         Wscript.Echo "Service with name " & strService & " not found on computer " & strComputer
         WSCript.quit(99)
      End If

    End Sub
    '---------------------------------------------------------------------------------------[/code]


  • 12.  [Solved] Windows service stop script - How to stop a windows service correctly

    Posted Dec 02, 2008 03:32 AM
    Hi Patrick,

    Great script you got there but how do I implement this in DU? Could you give me some hints?

    Stefan

    [quote name='Patrick D'haene' post='831' date='September 17 2008, 08:52 AM']Hi ,
       I've writing some time ago a vbscript program to restart a service on a computer.
       In this vbscript i stop the service then wait for a number of times the supplied time and if this time is reached and the service isn't stopped yet the script stops with an error.
      ps. :  in the script there is also the start of the service woth the same logic as above (use the parts of the scripts as needed).
    For this code to run you need to install Windows Script Host Version 5.6.
    Code :
    [code]' ReStartService.vbs
    ' Sample script to Stop or Start a Service
    ' -------------------------------------------------------'

    Option Explicit

    Dim objWMIService, objItem, objService
    Dim colListOfServices, strComputer, strService, intSleep
    On Error Resume Next
    ' --------------------------------------------------------------------------
    If Wscript.Arguments.Count < 2 Then
      wscript.Echo "Arguments incorrect 2 parameters required. " & vbCrLf _
         & "Parameter 1 = ComputerName, Parameter 2 = ServiceName, Parameter 3 = Wait time in seconds (default 15 sec)"
      Wscript.Quit(8)
    end if
    ' ---- computer name
    strComputer = Wscript.Arguments(0)
    ' ---- NB strService is case sensitive.
    strService = "'" & Wscript.Arguments(1) & "'"
    ' ---- wait time
    If Wscript.Arguments.Count =3 Then
      intSleep = Cint(Wscript.Arguments(2))
    else
      intSleep = 15
    end if
    Wscript.echo " ComputerName       = " & strComputer
    Wscript.echo " Service             = " & strService
    Wscript.echo " Wait time in Sec  = " & intSleep
    intSleep = intSleep *  1000
    ' --------------------------------------------------------------------------
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    if err.number <> 0 then
      Wscript.Echo "Error connecting to computer : " & err.description
      wscript.quit(99)
    end if
    ' --------------------------------------------------------------------------
    getServiceData
    ' --------------------------------------------------------------------------
    For Each objService in colListOfServices
    '     Wscript.Echo "Before stop " & objService.DisplayName  & VbTab & objService.State
      if objService.State <> "Stopped" Then
         objService.StopService()
         if err.number <> 0 then
           Wscript.Echo "Error stopping service : " & err.description
           wscript.quit(99)
         end if
      end if
    Next
    ' --------------------------------------------------------------------------
    Dim ind, nbrRetry
    ind=1
    nbrRetry=0
    While ind=1
    '  Wscript.Echo "Waiting for 15000 milleseconds"
      WSCript.Sleep intSleep
      nbrRetry=nbrRetry+1
      getServiceData
      For Each objService in colListOfServices
         Wscript.Echo "Object state " & objService.State
         if objService.State = "Stopped" Then
           ind=0
         else
           if nbrRetry>5 Then
              Wscript.Echo "Error Service did not stop within reasonabel time ! "
              wscript.quit(99)
           end if
         end if
      Next
    Wend
    ' --------------------------------------------------------------------------
    For Each objService in colListOfServices
    '     Wscript.Echo "Before start " & objService.DisplayName  & VbTab & objService.State
         objService.StartService()
    Next
    ' --------------------------------------------------------------------------
    ind=1
    nbrRetry=0
    While ind=1
    '  Wscript.Echo "Waiting for 15000 milleseconds"
      WSCript.Sleep intSleep
      nbrRetry=nbrRetry+1
      getServiceData
      For Each objService in colListOfServices
         Wscript.Echo "Object state " & objService.State
         if objService.State = "Running" Then
           ind=0
         else
           if nbrRetry>5 Then
              Wscript.Echo "Error Service did not start within reasonabel time ! "
              wscript.quit(99)
           end if
         end if
      Next
    Wend
    ' --------------------------------------------------------------------------
    WScript.Echo "Your "& strService & " service has Restarted"

    WScript.Quit
    ' --------------------------------------------------------------------------
    '------------------------ get service data ---------------------------------
    Sub getServiceData()
      Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name =" & strService & " ")
      if err.number <> 0 then
         Wscript.Echo "Error getting service data : " & err.description
         wscript.quit(99)
      end if
    ' --------------------------------------------------------------------------
      If colListOfServices.Count = 0 then
         Wscript.Echo "Service with name " & strService & " not found on computer " & strComputer
         WSCript.quit(99)
      End If

    End Sub
    '---------------------------------------------------------------------------------------[/code][/quote]


  • 13.  [Solved] Windows service stop script - How to stop a windows service correctly

    Posted Dec 04, 2008 03:58 AM
    [quote name='bigfootxl' post='1178' date='December 2 2008, 09:31 AM']Hi Patrick,

    Great script you got there but how do I implement this in DU? Could you give me some hints?

    Stefan[/quote]

    Hello,

    You would need to create a CL_INT uproc, with a script that will look likes :
    [code]echo test
    cscript.exe test.vbs nodename servicename 30
    set resexe=%ERRORLEVEL%[/code]
    For example :
    [code]echo test
    cscript.exe test.vbs frwpmglc INT533_frwpmglc_ORD_X 30
    set resexe=%ERRORLEVEL%[/code]

    it will shutdown the service INT533_frwpmglc_ORD_X and wait for 30 seconds.

    What I did here is the simpliest script possible, and I would not recommend to do that in production /wink.png' class='bbc_emoticon' alt=';)' />
    I think that in production you will need to add way more tests.

    For example, add a timeout on your VBS execution.
    It will kill the vbs script if it hangs for too long (depending on the timeout value you provided), by adding the parameter:
    //T:%TIMEOUT%

    [code]echo test
    set TIMEOUT=200
    cscript.exe test.vbs frwpmglc INT533_frwpmglc_ORD_X 30 //T:%TIMEOUT%
    set resexe=%ERRORLEVEL%[/code]

    Or you could also add some more tests like :
    check if the service is started before trying to shutdown him
    check if any jobs are running ...

    etc /smile.png' class='bbc_emoticon' alt=':)' />
    I'm sure Professionnal Services could also help you there

    Guillaume