Jump to content
Sign in to follow this  

Graceful exit of Autoit scripts

Recommended Posts


I'm not sure if this comes under "exception handling" but results when I searched for exception handling were a whole lot different, so i thought I'll start a new thread.

I have 50+ scripts and I would like to run them in sequence. Lets assume I use a shell script to run them. Assuming script 25 fails. (By fail, i mean a condition where some control has changed and the script cannot find a certain element and so the script just gets stuck), how do I get script 25 to gracefully exit and let script 26 run?

Or as an alternative, can i have a global time out which tells AutoIt,wait for a maximum of 2 minutes before giving up?

Any help is much appreciated!


Share this post

Link to post
Share on other sites


You can check if the function succeeds or not by checking its return value or the error/extended macros.

Then it's up to you to do the rest.

Br, FireFox.


OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control


Share this post

Link to post
Share on other sites

Hey FireFox,

Thank you for your reply! Yeah, thats one possibility, but each script has 100s of code with numerous functions and to check each one could be time consuming. But well, if there is no other option then :)

I was hoping for a more global option that can be set. (like WinWaitDelay).

Btw, I was wondering why does the script freeze when it is unable to perform an operation - say if a button is not present and the script tries to click it. Can it throw an exception saying "hey no button found". Any ideas as to making a global change?

Share this post

Link to post
Share on other sites


Not sure what you mean by "gracefully".  If you just want to cancel the script based on a timeout and run the next script then the following might be useful to you. 

The script sets up a test envorinment:

   @scriptdirtest scripts*.au3


        Several scripts that will run for varying lengths of time.

        One of the scripts is setup to exceed the 120 second timeout limit.

   @scriptdirtest scriptsrun_log.txt

        Logging mechanism used for testing.

The driver (shell) portion of the script  works off a timer and the PID.  Scripts are run using "autoit3.exe scriptname".  The scriptnames are fed to the driver from the array created at the top of the script.

You may be able to use this as a model to set something up for your environment.

#include <array.au3>
#include <date.au3>

; Hammer and Tongues Method

;  Create Test Environment

local $aScripts[50] =   ['Script1.au3', _
                        'Script2.au3', _
                        'Script3.au3', _
                        'Script4.au3', _
                        'Script5.au3', _
                        'Script6.au3', _
                        'Script7.au3', _
                        'Script8.au3', _
                        'Script9.au3', _
                        'Script10.au3', _
                        'Script11.au3', _
                        'Script With Timeout Problem.au3', _
                        'Script21.au3', _
                        'Script22.au3', _
                        'Script23.au3', _
                        'Script24.au3', _
                        'Script25.au3', _
                        'Script26.au3', _
                        'Script27.au3', _
                        'Script28.au3', _

local $ret = 0, $test_path = '\Script Test'
if fileexists(@scriptdir & $test_path) then filedelete(@scriptdir & $test_path)
$ret = DirCreate(@scriptdir & $test_path)
if $ret <> 1 then
    ConsoleWrite('DirCreate for test script directory failed...exiting...' & @LF)

local $str = ''

l('Generating Scripts')

for $1 = 0 to ubound($aScripts) - 1

    if $aScripts[$1] = '' then exitloop

    $str = '#include <date.au3>' & @lf

    if $aScripts[$1] = 'Script With Timeout Problem.au3' then
        $str &= 'sleep(999999)' & @lf
        $str &= 'local $sleep_time = random(1,10,1) * 1000' & @lf
        $str &= 'l("Script = " & @scriptname & " running for " & $sleep_time/1000 & " seconds")' & @lf
        $str &= 'sleep($sleep_time)' & @lf
    $str &= 'func l($str)' & @lf
    $str &= '   local $hfl = fileopen(@scriptdir & "\run_log.txt",1)' & @lf
    $str &= '   filewrite($hfl,stringformat("%-20s    %-100s",_now(),$str) & @lf)' & @lf
    $str &= '   fileclose($hfl)' & @lf
    $str &= '   $hfl = 0' & @lf
    $str &= 'endfunc' & @lf

    $ret= filewrite(@scriptdir & $test_path & '\' & $aScripts[$1],$str)
    if $ret <> 1 then
        ConsoleWrite('File write for script = ' & @scriptdir & $test_path & '\' & $aScripts[$1] & @LF)


func l($str)

    local $hfl = fileopen(@scriptdir & $test_path & '\run_log.txt',1) ; append
    filewrite($hfl,stringformat('%-20s    %-100s',_now(),$str) & @lf)
    $hfl = 0


;  Driver Script to run test scripts in order with a timeout value.

local $timeout = 120 ;  seconds
local $st, $pid

l('Start Driver Script.  Options:')
l(@tab & 'Timeout Value = ' & $timeout)

for $1 = 0 to ubound($aScripts) - 1

    if $aScripts[$1] = '' then exitloop

    l('Starting script = ' & $aScripts[$1])

    $pid = run('autoit3.exe ' & @scriptdir & '"' & $test_path & '"' & '\' & '"' & $aScripts[$1] & '"')
    ConsoleWrite($pid & @LF)

    $st = timerinit()

    while int(timerdiff($st)/1000) < 120
        if not processexists($pid) then exitloop

    if not processexists($pid) then
        l('Script ' & $aScripts[$1] & ' ended normally')
        l('Cancelling script = ' & $aScripts[$1] & ' due to timeout')


As to why your scripts might freeze.  As suggested, you need to interogate return codes for all functions.   Whenever you are doing something to a window you first need to verify it's presence and state.

I don't know of any global value that can be set to indicate "my script is fucking up".  The driver script above has no idea what the driven script is doing, only that it has exceeded a timeout value and the process id still exists.

Good Luck,


Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...