Sign in to follow this  
Followers 0
strate

Log exit of script

10 posts in this topic

I have a problem, I need to have my script add to a log file when the user exits the script. It needs to know if the user went into the task manager and ended process on itself also. I don't want to use two scripts. Any thoughts?

While 1
    Sleep(25)
    ToolTip('OnExit')
WEnd

Func OnAutoItExit ( )
    ToolTip('')
    MsgBox(0,'','Exit')
EndFunc

INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station...

Share this post


Link to post
Share on other sites



_FileWriteLog()

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

_FileWriteLog()

8)

I understand how to write logs but i want to know how to find out if the user ended the process thru the task manager.

INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station...

Share this post


Link to post
Share on other sites

Log a graceful exit in your OnAutoItExit function. (Log file, registry, whatever)

If the flag doesn't exist, the process was killed or is still running.


[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites

Log a graceful exit in your OnAutoItExit function. (Log file, registry, whatever)

If the flag doesn't exist, the process was killed or is still running.

In the little example I was using to test ideas when I killed the process thru the task manager It never gave me the message box.

INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station...

Share this post


Link to post
Share on other sites

you need to use the on exit option... but through the task manager it still wont work correctly

#include<file.au3>

Opt("OnExitFunc","OnAutoItExit");"OnAutoItExit" called

While 1
    Sleep(25)
    ToolTip('OnExit')
WEnd

Func OnAutoItExit ( )
    _FileWriteLog(@ScriptDir & "\Logout.txt", "Exit #" & @exitCode)
    ToolTip('')
   ;MsgBox(0,'','Exit  #' & @EXITMETHOD)

EndFunc

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

you need to use the on exit option... but through the task manager it still wont work correctly

#include<file.au3>

Opt("OnExitFunc","OnAutoItExit");"OnAutoItExit" called

While 1
    Sleep(25)
    ToolTip('OnExit')
WEnd

Func OnAutoItExit ( )
    _FileWriteLog(@ScriptDir & "\Logout.txt", "Exit #" & @exitCode)
    ToolTip('')
 ;MsgBox(0,'','Exit  #' & @EXITMETHOD)

EndFunc

8)

You don't need the Opt('OnExitFunc') if your using the function OnAutoItExit(), only if your going to name that function something else.
MsgBox(0, 'Example', 'When you click OK, you will be taken to the exit function')

Func OnAutoItExit()
    MsgBox(0, 'End of Example', 'See ya')
EndFunc

Edit:

@strate - I know you don't want it, but I can't see any other way than to have another script/exe watch it for the process to be killed by task manager or any other method (some other process closing it).

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I'm thinking this is something I'll have to give up on.....

@SmOke I knew that watching the process list would solve it but I want to avoid that as much as possible.

Is it possible that windows creates a log somewhere??

Edited by strate

INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station...

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I'm thinking this is something I'll have to give up on.....

@SmOke I knew that watching the process list would solve it but I want to avoid that as much as possible.

Is it possible that windows creates a log somewhere??

Well, I know you said you didn't want to, I don't understand why to be honest, and you've provided no explination one way or the other. If you don't want to run 2 scripts than this will be useless to you, but... try shutting this down without getting logged (If you run this from SciTe, then change ProcessList(@ScriptName) to ProcessList('AutoIt3.exe')).

While 1
    If ProcessExists('taskmgr.exe') And UBound(ProcessList(@ScriptName)) - 1 < 2 Then 
        _SecureExitMethod(@TempDir & '\WatchEXE.txt', @TempDir & '\BackUpExit.txt')
    EndIf
    Sleep(10)
WEnd

Func _SecureExitMethod($LaunchFile, $FileToWriteTo)
    Local $ThisPID = EnvSet('CurrentPID', @AutoItPID)
    Local $sExitLine[10] = ['#NoTrayIcon', 'Global $WatchPID = EnvGet("CurrentPID")', 'While ProcessExists($WatchPID)', _
        '   Sleep(100)', 'WEnd', 'If ProcessExists("taskmgr.exe") Then', '  $FileOpen = FileOpen("' & $FileToWriteTo & '", 2)', _
        '   FileWriteLine("' & $FileToWriteTo & '", ' & '@Mon & "/" & @MDay & "/" & @Year & @CRLF & @Hour & ":" & @Min & ":" & @Sec)', _
        '   FileClose($FileOpen)', 'EndIf']
    $FileOpen = FileOpen($LaunchFile, 2)
    For $iCount = 0 To UBound($sExitLine) - 1
        FileWriteLine($LaunchFile, $sExitLine[$iCount])
    Next
    FileClose($FileOpen)
    $WatcherPID = Run(@AutoItExe & ' /AutoIt3ExecuteScript ' & $LaunchFile)
EndFunc

Func OnAutoItExit()
    Local $aExitMethods[5] = ['Natural closing', 'close by Exit function', _
        'close by clicking on exit of the systray', 'close by user logoff', 'close by Windows shutdown']
    MsgBox(0, 'End of Example', 'Exe was closed by: ' & $aExitMethods[@exitMethod])
EndFunc

Now, with that being said, then on the next start up, you would simply check the 2nd text doc, to see if there was a time there, and if there was, append it to the File that you would normally write too that has a blank exit in it.

Edit:

Posted the wrong example.

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Well, I know you said you didn't want to, I don't understand why to be honest, and you've provided no explination one way or the other. If you don't want to run 2 scripts than this will be useless to you, but... try shutting this down without getting logged (If you run this from SciTe, then change ProcessList(@ScriptName) to ProcessList('AutoIt3.exe')).

While 1
    If ProcessExists('taskmgr.exe') And UBound(ProcessList(@ScriptName)) - 1 < 2 Then 
        _SecureExitMethod(@TempDir & '\WatchEXE.txt', @TempDir & '\BackUpExit.txt')
    EndIf
    Sleep(10)
WEnd

Func _SecureExitMethod($LaunchFile, $FileToWriteTo)
    Local $ThisPID = EnvSet('CurrentPID', @AutoItPID)
    Local $sExitLine[10] = ['#NoTrayIcon', 'Global $WatchPID = EnvGet("CurrentPID")', 'While ProcessExists($WatchPID)', _
        '   Sleep(100)', 'WEnd', 'If ProcessExists("taskmgr.exe") Then', '  $FileOpen = FileOpen("' & $FileToWriteTo & '", 2)', _
        '   FileWriteLine("' & $FileToWriteTo & '", ' & '@Mon & "/" & @MDay & "/" & @Year & @CRLF & @Hour & ":" & @Min & ":" & @Sec)', _
        '   FileClose($FileOpen)', 'EndIf']
    $FileOpen = FileOpen($LaunchFile, 2)
    For $iCount = 0 To UBound($sExitLine) - 1
        FileWriteLine($LaunchFile, $sExitLine[$iCount])
    Next
    FileClose($FileOpen)
    $WatcherPID = Run(@AutoItExe & ' /AutoIt3ExecuteScript ' & $LaunchFile)
EndFunc

Func OnAutoItExit()
    Local $aExitMethods[5] = ['Natural closing', 'close by Exit function', _
        'close by clicking on exit of the systray', 'close by user logoff', 'close by Windows shutdown']
    MsgBox(0, 'End of Example', 'Exe was closed by: ' & $aExitMethods[@exitMethod])
EndFunc

Now, with that being said, then on the next start up, you would simply check the 2nd text doc, to see if there was a time there, and if there was, append it to the File that you would normally write too that has a blank exit in it.

Edit:

Posted the wrong example.

You are truly amazing! It work GREAT! Even though it is still two EXE's it is only compiled as one. The reason I was being anal about the two exe's was cause I wanna make it easy for my company to install and operate my scripts. But I wanna be able to log every action that is made associated with them, for troubleshooting purposes.

Thank you again!


INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station...

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