Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.5: Use launchd to restart crashed apps System 10.5
launchd in 10.5 introduces a couple of new flags to the launchd config plists, specifically KeepAlive (bool or dict) and SuccessfulExit (bool, as child of KeepAlive dict). Using these flags, you can tell launchd to launch an application and automatically restart it if it crashes, but NOT if it exits cleanly. This previous hint showed a somewhat different approach to accomplish the same thing.

Using my company's flight management software as an example, you would end up with the following LaunchAgent .plist file, which I put in my home Library/LaunchAgents folder:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>KeepAlive</key>
	<dict>
		<key>SuccessfulExit</key>
		<false/>
	</dict>
	<key>Label</key>
	<string>com.frontierflying.flightmaster</string>
	<key>ProgramArguments</key>
	<array>
		<string>/Users/Shared/FlightMasterStandalone/FlightMaster2004.app/Contents/MacOS/EXE</string>
	</array>
</dict>
</plist>
I made the inital file using Lingon, and then edited it using Propertylist editor to add the KeepAlive dict/SuccessfulExit key set. The program arguments string needs to be the path to the actual executable inside the app bundle, not just to the .app (assuming it is a bundle app) as shown.

Of course, this loads immediately at login. If this is the desired behavior, then you are done. If you don't want this, then put the .plist file in your documents directory or something, and use something like the following two-line script to load it as desired:
launchctl unload /path/to/launchagent.plist
launchctl load /path/to/launchagent.plst
The unload command is needed only if you want to launch the application multiple times in a session. From what I can tell, once loaded, the LaunchAgent remains loaded, and simply stops monitoring the app after it quits cleanly -- but doesn't unload.

In order to relaunch the app, you first have to unload the LaunchAgent, the load it again, thus the first line of the script. That script should be able to be put into an AppleScript using the do shell script AppleScript command if you want (although I didn't test this), or just leave it as a Terminal script. Then just run that script, and launchd will launch your application, monitor it, and restart it if it crashes, but not if it exits cleanly.
    •    
  • Currently 3.20 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (5 votes cast)
 
[21,461 views]  

10.5: Use launchd to restart crashed apps | 3 comments | Create New Account
Click here to return to the '10.5: Use launchd to restart crashed apps' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.5: Use launchd to restart crashed apps
Authored by: mlhoenig on Dec 23, '07 11:39:04AM
OK...being rather Terminal-impaired and mostly a hardware nut (though I've been a Mac user since '89 or so) I'm lost regarding exactly how to modify this for Safari and Mail, for instance.

Specifically, what do I name the .plist once I have it ready; and do I need separate files for each app I want to re-launch; I'm sure I can think of a few other questions, but these are the first two I absolutely need to understand in order to take advantage of this hint...

I've already created the following Plain-Text file using TextEdit:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">;
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
<key>Label</key>
<string>com.apple.safari</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/Safari.app/Contents/MacOS/Safari</string>
</array>
</dict>
</plist>

Thanks!

[ Reply to This | # ]
10.5: Use launchd to restart crashed apps
Authored by: Anonymous on Dec 23, '07 07:32:49PM

The file should be something akin to:

com.YOURSHORTUSERNAMEHERE.DescriptiveName.plist

So, for example, with my one for Quicksilver, I've named it:

com.mharris.Quicksilverwatch.plist

And, yes, you need separate files for each app you want to relaunch.



[ Reply to This | # ]
10.5: Use launchd to restart crashed apps
Authored by: nicwolff on Dec 26, '07 01:37:20PM

I think you may need one .plist file for each program - might as well do it that way just to be safe. Just name the text file "restart_safari.plist" or whatever, and put it in your ~/Library/LaunchAgents folder.



[ Reply to This | # ]