Sign in to follow this  
Followers 0
Alek

Process watcher

4 posts in this topic

#1 ·  Posted (edited)

a small script that allows you to add programs to a list so that if the program crashes the Process watcher restarts it.

#include <GUIConstants.au3>

;Declear all $Programs and creat all the needed arrays
Global $Programs = 1
Global $Group[20]
Global $Input[20]
Global $Button[20][2]
Global $Process[20]

;To create a window bar on the startmenu because we are using $WS_EX_TOOLWINDOW in the second gui
;We are hiding this one outside the screen
$Task_Bar = GUICreate("",20,20,-100,-100)
GUISetState()

$Form1 = GUICreate("", 295, 100,-1,-1,-1,$WS_EX_TOOLWINDOW,$Task_Bar)
$Group[$Programs] = GUICtrlCreateGroup("Program " & $Programs, 5, 5, 285, 70)
$Input[$Programs] = GUICtrlCreateInput("", 10, 20, 245, 25)
$Button[$Programs][0] = GUICtrlCreateButton("...", 260, 20, 25, 25, 0)
$Button[$Programs][1] = GUICtrlCreateButton("Start", 10, 50, 276, 21, 0)
$Button_Add = GUICtrlCreateButton("Add a Process",5,75,285,20)

;set the resizing so that they dont move when the gui changes size
GUICtrlSetResizing($Group[$Programs],802)
GUICtrlSetResizing($Input[$Programs],802)
GUICtrlSetResizing($Button[$Programs][0],802)
GUICtrlSetResizing($Button[$Programs][1],802)
GUICtrlSetResizing($Button_Add,$GUI_DOCKBOTTOM+$GUI_DOCKHEIGHT)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button_Add
            
            ;because i only decleard the arrays to 20 i needed to set a limit
            If $Programs < 20 Then
                
                ;Get the gui pos
                $Pos = WinGetPos($Form1)
                
                ;change the gui size to make room for the new controls
                WinMove($Form1,"",$Pos[0],$Pos[1],$Pos[2],$Pos[3]+75)
                
                ;set programs to +1
                $Programs += 1
                
                ;create the new controls at the new spot
                ;((75*$Programs)-75) creates a base position and the +# sets the position of the control
                $Group[$Programs] = GUICtrlCreateGroup("Program " & $Programs, 5, (75*$Programs)-75, 285, 70)
                GUICtrlSetResizing(-1,802)
                $Input[$Programs] = GUICtrlCreateInput("", 10, ((75*$Programs)-75)+20, 245, 25)
                GUICtrlSetResizing(-1,802)
                $Button[$Programs][0] = GUICtrlCreateButton("...", 260, ((75*$Programs)-75)+20, 25, 25, 0)
                GUICtrlSetResizing(-1,802)
                $Button[$Programs][1] = GUICtrlCreateButton("Start", 10, ((75*$Programs)-75)+50, 276, 21, 0)
                GUICtrlSetResizing(-1,802)
            EndIf
    EndSwitch
    
    ;check and see if any of the buttons was pressed
    For $x = 1 To $Programs
        
        ;Check if message = the .../browse button
        If $nMsg = $Button[$x][0] Then
            
            ;open the select file dialog
            $Dir = FileOpenDialog("","","(*.exe)")
            
            ;Set the data of the input to the new file
            GUICtrlSetData($Input[$x],$Dir)
        EndIf
        
        ;check if the Start/Stop button was pressed
        If $nMsg = $Button[$x][1] Then
            
            ;if the text on the start/stop button = start then
            If GUICtrlRead($Button[$x][1]) = "Start" And GUICtrlRead($Input[$x]) <> "" Then
                
                ;run the program and set the PID to $Process[$x]
                $Process[$x] = Run(GUICtrlRead($Input[$x]))
                
                ;change the button text to stop
                GUICtrlSetData($Button[$x][1],"Stop")
            Else ;else if the button text = Stop then
                
                ;Change the button text back to start
                GUICtrlSetData($Button[$x][1],"Start")
                
                ;Kill the process and wait for it to close
                While ProcessExists($Process[$x])
                    ProcessClose($Process[$x])
                    Sleep(1)
                WEnd
            EndIf
        EndIf
        
        ;Might as well throw in the restart function
        ;check if the text on the button = "stop" and if the processes is not runing and if the input is not empty
        ;also check if the file exists
        If GUICtrlRead($Button[$x][1]) = "Stop" And Not ProcessExists($Process[$x]) And GUICtrlRead($Input[$x]) <> ""  And FileExists(GUICtrlRead($Input[$x])) Then
            
            ;Start a timer
            $Timer = TimerInit()
            
            ;run the program and set $Process[$x] to the new PID
            $Process[$x] = Run(GUICtrlRead($Input[$x]))
            
            ;Wait for it to start properly
            Do
                Sleep(1)
            Until ProcessExists($Process[$x]) Or TimerDiff($Timer) > 30000
            
            ;If the timer is more then 30sec then there is proberly something wrong and stops the processes
            ;And sets the buttons text to Start to keep it from trying to restart it again
            If TimerDiff($Timer) > 30000 Then
                GUICtrlSetData($Button[$x][1],"Start")
            EndIf
        EndIf
    Next
WEnd
Edited by Alek

[font="Impact"]Never fear, I is here.[/font]

Share this post


Link to post
Share on other sites



very nice :) i like when u click add program the gui just expands.. looks cool :) this is gonna be usefull for me ^_^


http://www.autoitscript.com/forum/index.php?showtopic=69911 <-- Best hacker ever :D

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