Jump to content

Test for program abend and restart the program


helmar
 Share

Recommended Posts

I work in endpoint security, and one of our products is currently being "blamed" for impacting Cisco Jabber on our endpoints.  To support that testing, I have Jabber running on my box.  An unrelated Jabber issue is it closes randomly.  They, Cisco, supposedly are looking at that.  One item they want is logs of when it goes down.  One item I want is for it to stay up.

I wrote this script to watch if it goes down, log it, and bring it back up.  Is there a better way?  Also, when I review my log file, it seems that my script starts Jabber twice.  I think my timing (WinWaitDelay) is off, but since I am in a While WEnd, not sure how/where to tweak or fix it.  I also want to add a tray menu that single primary click shows normal menu and double primary opens the log file, but I am nor sure where to place that in the code relative to the looping.

Thanks

#Region includes, options, & variables
#Region includes
#include <Date.au3>
#EndRegion
#Region options
Opt("WinWaitDelay", 30000) ;30 seconds, default is 250 milliseconds
Opt('MustDeclareVars', 1) ;1 = Variables must be pre-declared.
Opt('TrayAutoPause', 0) ;Controls if script pauses when click on tray icon, 0 = no pause, 1 = (default pauses)
#EndRegion options
#Region global variables
Global $sLogFile, $sJabExe
Global $sScriptName = "Jabber Test"
Global $sSearchtext='Cisco Jabber'
Global $sLogFileDir = "C:\Users\Public\Documents"
Global $sLogFileName = "CiscoJabber.log"
Global $sLogFileTextDown = "######## Cisco Jabber is not running ########"
Global $sLogFileTextStart = "Started Jabber via the monitor app"
Global $sJabDir = "C:\Program Files (x86)\Cisco Systems\Cisco Jabber"
Global $sJabExeName = "CiscoJabber.exe"
#EndRegion global variables
#EndRegion includes, options, & variables

TraySetToolTip($sScriptName)
$sLogFile = $sLogFileDir & "\" & $sLogFileName
$sJabExe = $sJabDir & "\" & $sJabExeName
while 1
    If WinWaitClose($sSearchtext) > 0 Then
        ;intentionally calling by file name and not by handle since file open and close are both invoked if called by name and not by handle
        FileWriteLine($sLogFile, _NowCalc() & @MSEC & "; " & @ComputerName & "; " & @UserName & "; " & $sLogFileTextDown)
        Run($sJabExe,$sJabDir)
        FileWriteLine($sLogFile, _NowCalc() & @MSEC & "; " & @ComputerName & "; " & @UserName & "; " & $sLogFileTextStart)
    EndIf
wend

 

Link to comment
Share on other sites

  • Developers

I would think you need to give jabber time to start so put in a sleep after Run() or a WinWait().
You could then also capture the started PID by Run() and monitor that assuming it get killed and isn't hanging.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I do not understand why you have a default 30000 on WinWaitDelay and you check for @MSEC.  Seems to me you would want a shorter delay, so I put a 1 sec.

Instead of sleep I suggest you use WinWait (both ways are ok, but I prefer Win* funcs).  I made it worked with Notepad using CLASS instead of TITLE.

#include <Date.au3>
#include <Constants.au3>

Opt("WinWaitDelay", 1000) ;1 sec, default is 250 milliseconds
Opt('MustDeclareVars', 1) ;1 = Variables must be pre-declared.
Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 1)
Opt("TrayAutoPause", 0)

Global $sLogFile, $sJabExe
Global $sScriptName = "Jabber Test"
Global $sSearchtext = '[CLASS:Notepad]'
Global $sLogFileDir = @ScriptDir
Global $sLogFileName = "CiscoJabber.log"
Global $sLogFileTextDown = "######## Cisco Jabber is not running ########"
Global $sLogFileTextStart = "Started Jabber via the monitor app"
Global $sJabDir = @WindowsDir
Global $sJabExeName = "Notepad.exe"

TraySetToolTip("Left Click to exit" & @LF & "Right Click to see Log")
$sLogFile = $sLogFileDir & "\" & $sLogFileName
$sJabExe = $sJabDir & "\" & $sJabExeName

TraySetIcon("shell32.dll", "28")
TraySetOnEvent($TRAY_EVENT_PRIMARYUP, _Exit)
TraySetOnEvent($TRAY_EVENT_SECONDARYUP, _ShowLog)

While 1
  If WinWaitClose($sSearchtext) Then
    FileWriteLine($sLogFile, _NowCalc() & ":" & @MSEC & "; " & @ComputerName & "; " & @UserName & "; " & $sLogFileTextDown)
    Run($sJabExe, $sJabDir)
    If Not WinWait($sSearchtext, "", 15) Then Exit MsgBox($MB_SYSTEMMODAL, "", "Unable to restart " & $sJabExeName)
    FileWriteLine($sLogFile, _NowCalc() & ":" & @MSEC & "; " & @ComputerName & "; " & @UserName & "; " & $sLogFileTextStart)
  EndIf
WEnd

Func _Exit()
  Exit
EndFunc   ;==>_Exit

Func _ShowLog()
  ConsoleWrite("Show Log started ==========" & @CRLF)
  ConsoleWrite(FileRead($sLogFileName) & @CRLF)
  ; your code goes here
EndFunc   ;==>_ShowLog

 

Edited by Nine
Link to comment
Share on other sites

21 hours ago, Nine said:

If Not WinWait($sSearchtext, "", 15) Then Exit MsgBox($MB_SYSTEMMODAL, "", "Unable to restart " & $sJabExeName)

I have not seen that syntax before, If Then and two functions.

Thank you for your help and suggestions - I have incorporated them into my code (I swapped buttons, as my default is to check the logs, and most likely never exit while system is up and running).

Also, I now know how to TraySetIcon for uncompiled scripts.  I have only used non-default icons when I have compiled my scripts.

Link to comment
Share on other sites

  • Developers
1 minute ago, helmar said:

I have not seen that syntax before, If Then and two functions.

It is the same as this, so it's ok when you aren't testing the RC of your script execution as that is set to the returned value of the MsgBox() function:

If Not WinWait($sSearchtext, "", 15) Then 
    $rc = MsgBox($MB_SYSTEMMODAL, "", "Unable to restart " & $sJabExeName)
    Exit $rc
EndIf

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...