Jump to content

Obivous Exit is not obvious?


hannes08
 Share

Recommended Posts

Hey folks,

I have written a small script that stops a service and waits a predefined time for it to actually be stopped.

This script is compiled in CLI mode and is run by our scheduling system.

The output of the script indicates, that the service is stopped:

C:WINDOWSsystem32>C:jobsUniverseStopService.exe server1 servicex 300 
Starting (Version 1.0.0.7)
C:\Windows\System32\cmd.exe /c sc server1 query "servicex" | find /C /I "state"
Message after 0s: 0
Message after 1s: 0
Message after 2s: 0
Message after 3s: 0
Message after 4s: 0
Message after 5s: 0
Message after 6s: 0
Message after 7s: 0
Message after 8s: 1
Service stopped Successfully

This is the code part with the message:

If $b_stopped Then
    ConsoleWrite("Service stopped Successfully" & @CRLF)
    Exit 0
Else

Pretty obious that the script should end and set the returncode to "0", isn't it?

Yes, but the script doesn't stop after that. In some unregular cases (different executions, differrent servers) these scripts just keep on executing. They just don't stop.

Has anyone an idea what might go wrong here?

Complete script:

#include <Constants.au3>
#include <Timers.au3>

ConsoleWrite("Starting (Version " & FileGetVersion(@ScriptName) & ")" & @CRLF)
If $CmdLine[0] = 3 Or $CmdLine[0] = 4 Then
    If Ping($CmdLine[1]) > 0 Then
        ConsoleWrite(@ComSpec & " /c sc " & $CmdLine[1] & " query """ & $CmdLine[2] & """ | find /C /I ""state""" & @CRLF)
        $rc = Run(@ComSpec & " /c sc " & $CmdLine[1] & " query """ & $CmdLine[2] & """ | find /C /I ""state""", @ScriptDir, @SW_MINIMIZE, $STDERR_CHILD + $STDOUT_CHILD)
        $s_line = ""
        $s_rc = ""
        While 1
            $s_line = StdoutRead($rc)
            If @error Then ExitLoop
            If StringStripWS($s_line, 3) <> "" Then $s_rc = StringStripWS($s_line, 3)
        WEnd
        If StringIsInt($s_rc) And $s_rc > 0 Then
            $rc = Run(@ComSpec & " /c sc " & $CmdLine[1] & " stop """ & $CmdLine[2] & "", @ScriptDir, @SW_MINIMIZE)
            $t = _Timer_Init()
            $b_stopped = False
            While _Timer_Diff($t) / 1000 <= $CmdLine[3]
                $rc = Run(@ComSpec & " /c sc " & $CmdLine[1] & " query """ & $CmdLine[2] & """ | find /C /I ""stopped""", @ScriptDir, @SW_MINIMIZE, $STDERR_CHILD + $STDOUT_CHILD)
                $s_line = ""
                $s_rc = ""
                While 1
                    $s_line = StdoutRead($rc)
                    If @error Then ExitLoop
                    If StringStripWS($s_line, 3) <> "" Then
                        ConsoleWrite(@CRLF)
                        ConsoleWrite("Message after " & Floor(_Timer_Diff($t) / 1000) & "s: " & StringStripWS($s_line, 3) & @CRLF)
                        If StringStripWS($s_line, 3) = 1 Then
                            $b_stopped = True
                        EndIf
                    Else

                    EndIf
                WEnd
                If $b_stopped = True Then ExitLoop
                Sleep(1000)
            WEnd
            If $b_stopped Then
                ConsoleWrite("Service stopped Successfully" & @CRLF)
                Exit 0
            Else
                ConsoleWrite("Service was not stopped... Timeout encountered." & @CRLF)
                If $CmdLine[0] = 4 Then
                    ConsoleWrite("Running Emergency command: " & $CmdLine[4] & @CRLF)
                    If FileExists($CmdLine[4]) Then
                        $rc = RunWait($CmdLine[4])
                        If $rc = 0 Then
                            ConsoleWrite("Emergency command returned okay!" & @CRLF)
                            Exit 0
                        Else
                            ConsoleWrite("Emergency command returned an error: " & $rc & @CRLF)
                            Exit 6
                        EndIf
                    Else
                        ConsoleWrite("Command File not found! " & $CmdLine[4] & @CRLF)
                        Exit 5
                    EndIf
                EndIf
                Exit 4
            EndIf
        Else
            ConsoleWrite("Service seems not to be available on remote machine." & @CRLF)
            Exit 2
        EndIf
    Else
        ConsoleWrite("Remote machine not available!" & @CRLF)
        Exit 3
    EndIf
ElseIf $CmdLine[0] = 0 Then
    ConsoleWrite("No Parameter given. This could be by default." & @CRLF & "Exit" & @CRLF)
    Exit 0
ElseIf $CmdLine[0] = 1 Then
    ConsoleWrite("No Service specified. This could be by default." & @CRLF & "Exit" & @CRLF)
    Exit 0
Else
    If StringInStr($CmdLineRaw, '""') Then
        ConsoleWrite("No Service specified. This could be by default." & @CRLF & "Exit" & @CRLF)
        Exit 0
    EndIf
    ConsoleWrite("Wrong Parameter count." & @CRLF & "Expected: 3 or 4 (Servername, Service, Timeout [,cmd] )" & @CRLF & "Recieved: " & $CmdLine[0] & @CRLF)
    Exit 1
EndIf

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

I would add the process ID to the messages so you can check it is the same process which is still alive ...

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I once had a problem with multiple instances running unexpected and adding the PID to the messages helped me solve the problem.

Another cause might be a script error (just want to mention it but doesn't seem to be too likely in your case). Even for CLI executables a MsgBox is "displayed". As the exe runs in the background the MsgBox can't be seen and the script just hangs. Trancexx has posted a solution to this problem.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

is the thread I'm talking about.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Hopefully this solves your problem :D

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...