Creating a script that will execute a program and if it exits without crashing, will run it again I've already asked this question in the UNIx exchange but I was told to ask here also about AppleScripts if the standard shell does not work.

I require a script that will open an application. Wait for it to exit, if the application exited gracefully (I.E: did not crash) relaunch it. Otherwise, keep the crash report open and exit the script. I'd like this script to run until I quit it manually.

So far the following has been suggested:

while open -W /path/to/application.app
do
    :
done


But this opens the application and then either on crash or exit will automatically run it again. I assume this is because open regardless of waiting or not will terminate with a normal 0 exit code. Unless of course open crashes but in this case it will not.

Any assistance with this is appreciated.

Thanks.
To keep restarting the job on succesful exit You can use launchd on OSX. For this You have to prepare *.plist file in order to "tell" launchd how to behave.
To restart application on succesfull exit please look at KeepAlive -> SuccessfulExit key (more info: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html).
To start your job, You'll need to start it via launchctl command, for example:

launchctl load path/to/file.plist


To stop it:

launchctl unload path/to/file.plist


More informations about launchctl/launchd programming here:

http://alvinalexander.com/mac-os-x/launchd-examples-launchd-plist-file-examples-mac

http://launchd.info