10.5: Automatically restart a crashed program
Dec 19, '07 07:30:08AM
Contributed by: mark hunte
This older hint described a method by which to auto-restart a crashed application automatically by monitoring the crash log for the app. This was done by watching the log file with a LaunchAgent.
As WCityMike notes in this forum thread, Leopard writes multiple crash logs with a date and time-stamp in the filename for each crash, rather than appending to one log file as in Tiger. For example:
~/Library/Logs/CrashReporter/gimp-2.2_2007-12-10-100248_G4.crash
~/Library/Logs/CrashReporter/gimp-2.2_2007-12-10-091933_G4.crash
etc...
So watching a single file will not work in Leopard. Read on for my solution.
I wanted to keep true to the original tip and use Lingon to create the LaunchAgent (launchd) plist file, and only use that rather than creating a second file containing the script to be called by the LaunchAgent, which does the actual work of watching the files. However, Lingon for Leopard scrambles the plist with the commands needed -- it sees every space in the command as a newline, and also kills single quotes. So I made the plist using a plain text editor:
How to Build
Open a new Textedit document, and make it plain text if it is not already. Paste the above xml code into it. Replace the word yourLogname in the line <string>set appLogName to "yourLogname"</string>. The log file name should start with the app's name; my log files look like this: gimp-2.2_2007-12-10-100248_G4.crash -- so I use the text before the hyphen (ie I used gimp for my app name).
Replace the word yourappname in the line <string>tell application "yourappname" to activate</string> with your app's name. My app is called gimp, so I replace "yourappname" with "gimp".
Finally, replace the word USERNAME with your short user name in the <string>/Users/USERNAME... line. If my user name is John Smith, then normally my short username will be jsmith -- your home folder is normally named as your short user name. Save the file somewhere with the extension .plist (not .txt!). From here on, I will refer to this file as foo.plist.
Where to Place
I have used the LaunchAgents folder in the top-level /Library folder. This LaunchAgents folder is for all users rather than just me. You can use your Users one instead, but I have not tested this hint with that folder. Drag and drop the file into the folder /Library/LaunchAgents; you will be asked to authenticate as it is a protected folder, so do so using your Admin password.
Now some Terminal work is required. The plist files in the folder /Library/LaunchAgents are owned by system, so we need to set our file to the same owner. In Terminal, type this command, replacing foo.plist with your file's name:
sudo chown root: /Library/LaunchAgents/foo.plist
You will be asked to authenticate, so do so using your Admin password. Now load the LaunchAgent by typing this command, again replacing foo.plist with your file's name:
launchctl load /Library/LaunchAgents/foo.plist
If you do not want to load the LaunchAgent this way, then you can do so by logging out and back in again.
Notes:
A couple of things about the commands in the plist file. I used osascript, which allows AppleScript to be used from the command line. I chose osascript because I found that using unix shell commands to look for the new crash log file sometimes actually triggered the LaunchAgent. Also I found it easier. :-)
The LaunchAgent now looks at a folder rather than a file; this is called a watchPath. This means that if a file is moved in or out of this watchPath (folder), then the LaunchAgent will trigger. This can be a problem when a crash report file for another app is created. Or as I suspect will happen, the system tidies up the CrashReporter folder and removes files. However, the osascript in the LaunchAgent takes this into account and only reopens your application if the log file contains your app name and the file's modification date is in the last two minutes.
To uninstall the LaunchAgent, remove it from the LaunchAgents folder, and log out and back in. To just unload it, use Terminal and type:
launchctl unload /Library/LaunchAgents/foo.plist
Do not open/edit this file in Lingon; Lingon will scramble the file. I have tested this as much as I can, and it works for me.
Comments (10)
Mac OS X Hints
http://hints.macworld.com/article.php?story=20071210022032707