Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
    
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 at 11:09

3 Answers 3

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|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 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 at 21:51

Well, since you asked in UNIX - the "UNIX" way is:

while /path/to/application.app ; do
      echo "Restarting App"
      sleep 1
done
echo "Application has encountered an error!"

Or something like that.

share|improve this answer

I've tested this:

#!/bin/bash
while <your_cmd_here>
do
    true
done

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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