I'd like to know the easiest way of creating a bash script that will run an app in MacOSX (it's a UNIX based system so I assumed this was the right place). wait for the program to exit. If the exit wasn't a crash, run it again. Otherwise leave the crash report window open and exit.

I'd like this script to run forever until either the crash or manual termination.

I've a vague idea but not sure. Here is what I have so far:

echo "Launching Autorun…"

if ["$1" -eq "0"]; then
# Application exited successfully. Restarting.
# An error occured. Do nothing and exit.
  • you almost definitely don't want a script to run forever for any reason. you want a service - or a daemon - to run forever. I dunno how they do it on Apples, but you should probably look a little closer at your init system to find out if there is any way to register a service with it. Usually daemons rexec themselves periodically to update/save state - they don't loop forever, really. – mikeserv Jul 24 '14 at 11:09

This is what Bash while loops do:

while /path/to/application.app

It will run the application, and if it terminates successfully run the body of the loop. : is the shell's no-op command (the loop has to have a body, so that's what we put there); after that it goes back to the top and runs the program again. If it fails, the loop stops running and the script exits.

However, it looks like what you're running might be an ordinary Mac application (.app): there are a couple of issues that come up in that case. One is that you need to use the open command, rather than running the application directory directly: open /path/to/application.app.

The other is that when you do open will usually terminate immediately with a success, regardless of what the application goes on to do: that isn't absolutely universal, but most will. If your one does, you can use the -W option to force open to block until the application ends: open -W /path/to/application.app. Note that if the application was already running, this will wait until the existing execution terminates too.

How much of an issue any of that is depends on what appplication you're running. If it doesn't play nicely, doing this from the shell may not be the best option. In that case you're probably better off going with AppleScript, which you can ask about on Ask Different.

|improve this answer|||||
  • I've an app on mac that can intentionally crash that I've tested your above solution with. it will automatically restart the app even after crashing. Is there a way to check the exit of the application? as open will obviously terminate with an exit of 0 so will run again regardless. – Ben Crazed Up Euden Jul 24 '14 at 11:19
  • @BenCrazedUpEuden: My understanding is that open only returns failure for certain startup errors. Applications are started by a system service, not directly, so there's no direct communications channel back. Sometimes it's possible to start the application executable directly: /Applications/YourProgram.app/Contents/MacOS/YourProgram. Otherwise, we're drifting out of Unix and into places you'll get a better answer on Ask Different. You can run AppleScript from the shell with osascript – Michael Homer Jul 24 '14 at 21:51

I've tested this:

while <your_cmd_here>

This works by testing the return code of your command, POSIX style. 0 means Ok, anything else can be treated as an error.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.