#StandWithUkraine

Keeping software running with AutoIt

AutoIt ghacks has post on Application Monitor today which is simple program to keep applications running. It watches them and restarts if they are closed for any reason.

Since that is one of those small tasks that I prefer to code myself instead of having to use one more software title I made draft in AutoIt.

Step by step.

  1. Let’s define what we want to keep running and time between checks.
$path="e:\Install\_IcoFX\IcoFX.exe"
$tick=10
  1. Checking if that is real file.
If Not FileExists($path) Then Exit
  1. Since Windows identifies processes by executable name we need to get it out of path (and check if it is executable at all).
$executable=StringRegExp($path, ".*\\(.*\.exe)",1)
If @error Then Exit
$executable=$executable[0]

This code searches by regular expression that in human words means “everything after last \ if it ends with .exe”. Failure is detected as non-zero @error value. StringRegExp returns array but we only need single match so we take that from [0] position. Checking for errors is important here because working with non-array variable as with array is one of few things that instantly crash AutoIt. 4. Check and loop. Sleep takes milliseconds so we multiply by 1000.

While 1
If Not ProcessExists($executable) Then Run($path)
Sleep($tick*1000)
WEnd

Combining to single file : https://www.rarst.net/script/keeprunning.au3

There are obviously plenty of ways to enhance this like adding nice interface or reading list of applications to watch. But that depends on personal tastes. That’s power of scripting for yourself - you get what you need. :)

By the way I wanted to ask for some feedback on AutoIt posts. This was defined as secondary theme to blog from the beginning and I get constant search traffic on it. But I have some doubts.

Do you find it interesting? Or boring? Or have some task you want to script but scared to start? Tell me in the comments. :)

Related Posts

3 Comments

  • Nihar #

    Haven't developed this kind of script before. Nice script.
  • Frank #

    Autoit is a great resource. Thanks for providing your thoughts on the subject. Looking forward to seeing more of them.
  • Rarst #

    @Frank I am not posting about AutoIt actively (secondary topic at this blog), but I try to cover interesting tasks and stuff. See AutoIt tag for all posts.