views:

577

answers:

2

Hi Everyone:

I am wondering how to set Launchd to relaunch my app if the user has not chosen to choose "Quit" from the top bar. There are cases where something may happen to the app, and it may force quit, and if a preference is selected I want it to relaunch automatically. I have looked into it before posting here, and I have heard that Launchd would be pretty good at doing this. Does anyone have any advice about how to accomplish it?

Thanks for any help.

+2  A: 

Let your program write an empty .something file in the user's home folder when the app is running. Remove the .something file when the application quits normally. Now you could write a little script (AppleScript or shell script) that checks if your app is running and in case it is not if it should (by checking the .something file). The script can then, if needed restart the app. You can schedule the script to run automatically at startup using launchd.

Diederik Hoogenboom
Hi Diederik:Good idea, however I have one question about that. That question being: When should I run the script? Can I set it to monitor if the app somehow closes, or something like that?
PF1
The script should be running all the time. Within the script you should use a sleep time (like a Unix sleep(10) ) so that the script will not use too much CPU. Instead of Launchd you can also add the script in the user's login items this way the preference setting will only have an effect for the current user (which should be the preferred way I guess).
Diederik Hoogenboom
Thanks for that advice Diederik. So, are you saying that the script should be checking for that file every 10 seconds? And then if it was there it would try to open the app (and if the app was open, then it wouldn't appear as if anything happened). But, and I'm assuming there isn't, is there a way to get notifications if the app quits? It seems as if this would be easier.
PF1
launchd controls Launch Agents (which are per user) as well as Daemons, so whether or not you want it to only apply to one use account shouldn't be a consideration in your choice.
smorgan
+2  A: 

You can use the SuccessfulExit setting of KeepAlive to only relaunch the app if it didn't quit normally. See this Mac OS X Hints entry, and/or the launchd.plist man page, for details.

smorgan
Hi smorgan: Thanks for the link. It looks really good!
PF1