Sign in to follow this  
Followers 0
hannes08

Obivous Exit is not obvious?

8 posts in this topic

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]

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Hi water,

well it is, as when I manually exit the script the scheduler just continues with it's part as if it just would have exited.


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Just to get you right: There's always a hidden MsgBox? oO

Okay, I'll see if I can find what you mentioned on the forum.


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites
is the thread I'm talking about.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thanks for the link! I have implemented the function into my program and will test and monitor it during the next days.


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

Hopefully this solves your problem :D


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Similar Content

    • IamKJ
      By IamKJ
      So I have a lot of different functions in the little program I'm making.  Most of them use the Do statement, such as finding images, clicking buttons, etc.
       
      The question I have is how would I be able to both
      1) Create a hotkey to pause whatever I am doing at the moment.  If I have to put something into every function in the program, I don't mind.
      2) Create a hotkey to exit whatever I am doing and stop all loops or commands and bring back the main GUI?
    • ur
      By ur
      I have created an exit function and registered using OnAutoItExitRegister.But it is not running when I close the app from taskbar or even with normal exit during end.
       
      Below is the part of code and the included files.
      #include <MsgBoxConstants.au3> #include <FileConstants.au3> #include <WinAPIFiles.au3> #include <Date.au3> #include <file.au3> #include <Array.au3> #include <String.au3> OnAutoItExitRegister("Exit_Program") Func Exit_Program() LoggingMainScr("Entered Exit_Program") Local $exitmethod = ["Natural closing", "close by Exit function", "close by clicking on exit of the systray","close by user logoff","close by Windows shutdown"] ExitProgram("Exit with exit code: "& @exitCode & " And because of: " & $exitmethod[@exitmethod],@exitmethod) EndFunc Func ExitProgram($sReason="Exit by user",$sCode=0) LoggingMainScr($sReason) if $sCode <> 0 then LoggingMainScr("stopped before completion") FileDelete(@ScriptDir&"\Machines*.txt") FileDelete(@ScriptDir&"\PsExec.exe") FileDelete(@ScriptDir&"\HyperVMachines.ps1") FileDelete(@ScriptDir&"\VMwareMachines.ps1") EndFunc Func LoggingMainScr($sMessage) LoggingDefault($sMessage,@ScriptDir&"\MainScr_Logging.log") EndFunc Func LoggingDefault($sMessage,$sLogFile=@ScriptDir&"\Logging.log") If $sMessage = "" Then FileWriteLine($sLogFile, "") Else FileWrite($sLogFile,_NowCalc() & " :: " & $sMessage&@CRLF) EndIf EndFunc Any suggestion.
       
    • fopetesl
      By fopetesl
      For some reason my personal notes were omitted when posting.
      Still a Newb it seems! So have attached my slightly modified example.
      If I comment out Exit button references script will not Exit even with windows 'X' button icon.
      If I'm losing GUI/Tab focus I cannot see where is the difference with(out) definition.
      GUI_No_Exit.au3
    • fosil
      By fosil
      Hi all,

      I'm working on a relatively complex automation program and I need it to call a specific function when it is closed by a user. Ive resorted to "OnAutoItExitRegister" as i believe that's the only option available.

      This command works perfectly fine in a small scale test application I wrote, but once implemented in the final program that's much larger and divided over many files in many directories it no longer works. I don't get any error messages.

      My question is has anyone encountered an issue where implementation of this command in a large scale program gave them trouble? Does it matter where "OnAutoItExitRegister" is called and where the function is located? Currently they are both places in the main file that runs the whole program but it still doesn't work.

      Any help is appreciated
    • Masum
      By Masum
      Hi all,
      Ctrl+Break stops script only if AutoIt window is active. It doesn't work while automation is going on elsewhere.
      Is there a way to stop the script regardless of where the automation is happening?
      Thanks