Jump to content

Multiple Process w/same name monitor/exe/loop...


Recommended Posts

I have a very basic process that I wrote and have been running for years that has worked great, but times have changed as well as needs. Basically the script runs in a loop looking for a process and then if the process does not exist, it starts it and re-checks in a minute.

While 1 ; Opens up a WHILE loop, with 1 as a constant, so it is infinite
If Not ProcessExists("myprog.exe") Then WinKill("WindowName")
If Not ProcessExists("myprog.exe") Then Run(@ComSpec & " /c MyBatch.bat ", "C:\TechScripts\", @SW_SHOW)
Sleep (60000) ; Puts the script to sleep for 60 Seconds so it doesn't chew CPU power
WEnd

I have to add complexity to this code now as I have a need to watch for 2 of the same process. Thinking about this I will need to find the process and count how many, if less than 2 then execute the WinKill and ComSpec, on next check is still not 2, execute it again. Same loop, same delay, same process, just 2 now.

My thought is that I need to create an array, or store into a variable the number of processes running as the process name. Something like:

While 1 ; Opens up a WHILE loop, with 1 as a constant, so it is infinite
$count = 2
$list = ProcessList("myprog.exe")
For $i = 0 To UBound($list) -2
Next
If $i < $count Then WinKill("DeadWindowName")
If $i < $count Then Run(@ComSpec & " /c MyBatch.bat ", "C:\TechScripts\", @SW_SHOW)
Sleep (60000) ; Puts the script to sleep for 60 Seconds so it doesn't chew CPU power
WEnd

I am not sure my logic is right here though. I know Ubound adds a number for each time it runs, so I am minus 2 to even the number to be accurate, however I am not sure the for loop will operate right in the while loop. Please help.

Link to comment
Share on other sites

Not sure this is the best way to do this, but it appears to work as designed. I only made minor changes to the above, and the order... otherwise, the end result is what I was looking for. I will check back to see if anyone else has replied. If you are wondering what this is for... it runs as a process that runs all the time that is watching for other processes to see if they die. If they do die, they are restarted with this process.

Opt("WinTitleMatchMode", 3) ;Match Window Title mode to be EXACT mode 3
Opt("TrayIconHide", 1) ;Hide Tray Icon

While 1 ;Opens up a WHILE loop, with 1 as a constant, so it is infinite
$count = 2 ;How many are suppossed top be running?
$list = ProcessList("myprog.exe") ;Place in an array all processes named "myprog.exe"
For $i = 0 To UBound($list) -2 ;Count items in array and store to variable -2 for Ubound restrictions
Next
If $i < $count Then Run(@ComSpec & " /c myprog.bat ", "C:TechScripts", @SW_SHOW) ;If count is less than $count variable start process
WinKill("DeadWindow") ;Kill Dead Window
Sleep (120000) ;Puts the script to sleep for 120 Seconds so it doesn't chew CPU power
WEnd
Edited by Tutungzone
Link to comment
Share on other sites

  • 1 month later...

Not sure this is the best way to do this, but it appears to work as designed. I only made minor changes to the above, and the order... otherwise, the end result is what I was looking for. I will check back to see if anyone else has replied. If you are wondering what this is for... it runs as a process that runs all the time that is watching for other processes to see if they die. If they do die, they are restarted with this process.

Opt("WinTitleMatchMode", 3) ;Match Window Title mode to be EXACT mode 3
Opt("TrayIconHide", 1) ;Hide Tray Icon

While 1 ;Opens up a WHILE loop, with 1 as a constant, so it is infinite
$count = 2 ;How many are suppossed top be running?
$list = ProcessList("myprog.exe") ;Place in an array all processes named "myprog.exe"
For $i = 0 To UBound($list) -2 ;Count items in array and store to variable -2 for Ubound restrictions
Next
If $i < $count Then Run(@ComSpec & " /c myprog.bat ", "C:TechScripts", @SW_SHOW) ;If count is less than $count variable start process
WinKill("DeadWindow") ;Kill Dead Window
Sleep (120000) ;Puts the script to sleep for 120 Seconds so it doesn't chew CPU power
WEnd

why you loop up to UBound($list) -2 ?

isn't it simpler as this single line?:

if UBound($list)-2 < $count then Run(......

instead of

For $i = 0 To UBound($list) -2 ;Count items in array and store to variable -2 for Ubound restrictions

Next

If $i < $count Then Run(@ComSpec

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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