Jump to content

Help with a script to check if multiple processes are running


PCGuy84
 Share

Recommended Posts

Hello,

I'm trying to build a script that will check if 3 processes are running and if any one of the processes is not I want to close the other 2 and run a script to restart them.  I'd like this to check the system every 10 minutes.  I've used AutoIT in the past to automate application installs and basic things and my "coding" background is limited so most likely it's something stupid I am doing so I'm hoping for someone to steer me in the right direction.  I've looked through the forums and google and was able to find some items to help such as the timer.  I'll post what I have below.

 

$StartTimer = TimerInit() ; Define the initial time we will be comparing to later
Checkprocess() ; Run our checkprocess() function on initial execute
While 1 ; Infinite Loop Condition is always true, you can exit these loops with "ExitLoop"
    If TimerDiff($StartTimer) > 600000 Then ; Only run the conditional code if the difference in time is greater than 1 min (600000 Miliseconds)
        Checkprocess()
    EndIf
    Sleep(10) ; So we don't kill the CPU
WEnd ; End of While Loop
Func Checkprocess()
If Not ProcessExists("Process1.exe") Then ProcessClose("Process2.exe") And ProcessClose("Process3.exe")
If Not ProcessExists("Process2.exe") Then ProcessClose("Process1.exe") and ProcessClose("Process3.exe")
If Not ProcessExists("Process3.exe") Then ProcessClose("Process1.exe") and ProcessClose("Process2.exe")
If Not ProcessExists("Process1.exe") and Not ProcessExists("Process2.exe") and Not ProcessExists("Process3.exe") Then Run("C:\Restart.bat"); if the processes don't exist, it starts the script
Sleep (10) ; Puts the script to sleep for 10 milliseconds so it doesn't chew CPU power
WEnd ; Closes the loop, tells it to go back to the beginning
EndFunc   ;==>Checkprocess

I get the following errors:

Syntax error multi-line 'If' missing 'Then' - I get this after the first 3 "If" statements
Syntax error - Statement cannot be just an expression. I get this after the "WEnd Statement"

 

I'm assuming it does not like my "And" statements but I'm not really sure how else to do it.  Again any helped would be appreciated.

Thanks

Link to comment
Share on other sites

Alright so it appears to be working except the batch script keeps opening over and over and over!  Here is the end of the script

 

If Not ProcessExists("Process1.exe") and Not ProcessExists("Process2.exe") and Not ProcessExists("Process3.exe") Then Run("C:\Restart.bat"); if the processes don't exist, it starts the script
EndFunc   ;==>Checkprocess

I removed the - 

Sleep (10) ; Puts the script to sleep for 10 milliseconds so it doesn't chew CPU power
WEnd ; Closes the loop, tells it to go back to the beginning" 

part as it was giving a syntax error I still have the Sleep and WEnd at the top of the scrip within the timer area.  It appears to do what I want it to do if all of the processes are closed it will open them all up and work, however if they are already open it is closing them and then just starts looping the start up of the batch file.

Link to comment
Share on other sites

Sorry I had just adjusted the bottom it was the same as the original I sent.  I added the Sleep(10000) and it helped no longer have the issue of it trying to open over and over, however now I'm finding that the first If statements seems to randomly close the processes.  

$StartTimer = TimerInit() ; Define the initial time we will be comparing to later
Checkprocess() ; Run our checkprocess() function on initial execute
While 1 ; Infinite Loop Condition is always true, you can exit these loops with "ExitLoop"
    If TimerDiff($StartTimer) > 600000 Then ; Only run the conditional code if the difference in time is greater than 1 min (600000 Miliseconds)
        Checkprocess()
    EndIf
    Sleep(10) ; So we don't kill the CPU
WEnd ; End of While Loop
Func Checkprocess()
If Not ProcessExists("Process1.exe") Then ProcessClose("Process2.exe") And ProcessClose("Process3.exe")
If Not ProcessExists("Process2.exe") Then ProcessClose("Process1.exe") and ProcessClose("Process3.exe")
If Not ProcessExists("Process3.exe") Then ProcessClose("Process1.exe") and ProcessClose("Process2.exe")
If Not ProcessExists("Process1.exe") and Not ProcessExists("Process2.exe") and Not ProcessExists("Process3.exe") Then Run("C:\Restart.bat"); if the processes don't exist, it starts the script
Sleep(10000) ; So we don't kill the CPU

EndFunc   ;==>Checkprocess

Due to the process closing issue even if the process did exist I tried another way.  I don't have the issue with the program restarting however if one process is closed it does not close the other two I have to manually close them all to get it to then restart.

$StartTimer = TimerInit() ; Define the initial time we will be comparing to later
Checkprocess() ; Run our checkprocess() function on initial execute
While 1 ; Infinite Loop Condition is always true, you can exit these loops with "ExitLoop"
    If TimerDiff($StartTimer) > 20000 Then ; Only run the conditional code if the difference in time is greater than 1 min (20000 Miliseconds)
        Checkprocess()
    EndIf
    Sleep(100) ; So we don't kill the CPU
WEnd ; End of While Loop
Func Checkprocess()
If Not ProcessExists("Process1.exe") Or Not ProcessExists("Process2.exe") Or Not ProcessExists("Process3.exe") Then ProcessClose("Process1.exe|Process1.exe|Process1.exe")
    Sleep(100) ; So we don't kill the CPU
If Not ProcessExists("Process1.exe") And Not ProcessExists("Process2.exe") And Not ProcessExists("Process3.exe") Then Run("C:\Script.bat"); if the processes don't exist, it starts the script
Sleep(10000) ; So we don't kill the CPU
EndFunc   ;==>Checkprocess

 

Link to comment
Share on other sites

PCGuy84,

This is one possible way to do this.  I am using an array to hold the process names.  This makes it easier to check if the process is running, close the processes and then check to make sure that the processes are closed...

; define array of processes to monitor
Local $aProcToMonitor = ['notepad.exe', 'winword.exe', 'wlmail.exe']

Local $LoopTime = 1000 * 60 * 1 ; time to wait between iterations of mainloop

Local $bProcessActive ; switch to indicate that a process is still active
Local $WaitTimeForProcessesToClose ; timer in case process does not close
Local $ret

; mainloop

While 1

    $bProcessActive = True
    Switch checkprocesses()
        Case 0
            MsgBox(0, '', 'Run your bat file here')
        Case 1
            MsgBox(0, '', 'Timed out waiting for processes to close')
        Case 2
            ConsoleWrite('All processes are running' & @CRLF)
    EndSwitch



    Sleep($LoopTime)

WEnd



Func checkprocesses()

    ; iterate array for each process
    For $i = 0 To UBound($aProcToMonitor) - 1
        ; if a process does not exist then close all processes in array
        If Not ProcessExists($aProcToMonitor[$i]) Then
            ConsoleWrite($aProcToMonitor[$i] & ' not running...attempting to close processes' & @CRLF)
            For $j = 0 To UBound($aProcToMonitor) - 1
                ProcessClose($aProcToMonitor[$j])
            Next
            ; wait for processes to close or timer to timeout
            $WaitTimeForProcessesToClose = TimerInit()
            While $bProcessActive

                $bProcessActive = False
                For $k = 0 To UBound($aProcToMonitor) - 1
                    If ProcessExists($aProcToMonitor[$k]) Then $bProcessActive = True
                Next
                ; return timeout signal
                If TimerDiff($WaitTimeForProcessesToClose) > 1000 * 60 * 1 Then Return 1
            WEnd

            ; return all processes closed signal
            If $bProcessActive = False Then Return 0
        EndIf

    Next
    ; return all processes running signal
    Return 2

EndFunc   ;==>checkprocesses

kylomas

edit: assumptions

1 - if any process is not active kill all processes in array

2 - there is only one of each process running

Edited by kylomas
additional info

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

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...