Creating a Bash script that will execute a program and if it exits without crashing, will run it again

30,676

This is what Bash while loops do:

while /path/to/application.app
do
    :
done

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.

Share:
30,676

Related videos on Youtube

Ben Crazed Up Euden
Author by

Ben Crazed Up Euden

Updated on September 18, 2022

Comments

  • Ben Crazed Up Euden
    Ben Crazed Up Euden almost 2 years

    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.
    /path/to/application.app
    else
    # An error occured. Do nothing and exit.
        exit
    fi
    
    • Admin
      Admin almost 10 years
      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.
  • Ben Crazed Up Euden
    Ben Crazed Up Euden almost 10 years
    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.
  • Michael Homer
    Michael Homer almost 10 years
    @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