I've looked all over for this and I'm not entirely sure how to do this.

I want to use a bash script to open an application using 'open' and then check the exit status of the application after it has finished.

As far as I am aware, open -W will exit with status 0 so long as the application did open, I'm not interested in this exit, I'm interested in the apps exit.

Currently I have the following script:

trap "echo manual abort; exit 1"  1 2 3 15; 
while open -W /path/to/MyApp.app
    echo "all is well"

exit 0 

Which opens the app and then waits, if the exit status is 0 it will re-open the app again after closing. This is intended however, what I want is to be able to stop this script if the app itself didn't exit 0 rather than the command 'open' successfully exiting.

So in short:

Open MyApp.app

if MyApp.app (not open) crashes, stop the script, otherwise, re-open MyApp.app

1 Answer 1


My answer is: you can't with open even with the -W flag. Rather use a shell script repeat_run which might be built as follows:

cat >repeat_run.sh <<'__eof__'
bin_name=`defaults read /Applications/${app_name}.app/Contents/Info.plist CFBundleExecutable 2>/dev/null`
if [ "${bin_name}" = "" ] ; then
    echo "${app_name} not found" >&2
    exit 2
trap "echo manual abort ; exit 1"  1 2 3 15
while : ; do
    case $rc in
        0)      echo "$1 terminated correctly" ;;
        *)      exit ${rc} ;;
make repeat_run
./repeat_run Pages
  • 1
    Executable name not always is the same as bundle name. It's better to read it from Info.plist. Something like that: APP_PATH="/Applications/Mail.app" echo "$APP_PATH"/Contents/MacOS/$(defaults read "$APP_PATH/Contents/Info.plist" CFBundleExecutable) Sep 4, 2014 at 7:45
  • 1
    Right. I fixed this and a problem with variables evaluation.
    – dan
    Sep 4, 2014 at 20:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .