The Program key in a launchd plist has the same semantics as execvp. From the launchd.plist manpage:
Program This key maps to the first argument of execvp(3). If this key is missing, then the first element of the array of strings provided to the ProgramArguments will be used instead.
According to the execvp manpage, a call to execvp uses the PATH environment variable if it is set. Otherwise, it searches /usr/bin and /bin. The absolute path is necessary because the directory containing the actual executable for a given application (usually /Contents/MacOS/) is unlikely to be in a user's PATH. Even if it were, it's possible that a user's PATH hasn't been set when launchd loads a given job (e.g. if .bash_profile is not read until after all launchd jobs have been loaded).
On another note, a big downside of the script above is that it cannot check the exit status of the application it is keeping alive. In other words, you cannot reliably quit an application (if you need to) without disabling the cron job.
Here are a couple of scripts that generate/install launchd jobs to relaunch a given application or executable if it exits unsuccessfully:
Terminal Tips: bash cron script to keep an app running
Oct 21st 2010 10:17PM (TUAW.com)<?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>Label</key>
<string>local.keep_dropbox_running</string>
<key>ProgramArguments</key>
<array>
<string>open</string>
<string>-W</string>
<string>-a</string>
<string>Dropbox</string>
</array>
<key>KeepAlive</key>
<true />
<key>RunAtLoad</key>
<true />
</dict>
</plist>
Terminal Tips: bash cron script to keep an app running
Oct 21st 2010 10:10PM (TUAW.com)<?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>Label</key>
<string>local.keep_dropbox_running</string>
<key>ProgramArguments</key>
<array>
<string>open</string>
<string>-W</string>
<string>-a</string>
<string>Dropbox</string>
</array>
<key>KeepAlive</key>
<true />
<key>RunAtLoad</key>
<true />
</dict>
</plist>
Terminal Tips: bash cron script to keep an app running
Oct 21st 2010 10:09PM (TUAW.com)*/5 * * * * /Users/luomat/bin/keep-my-app-running.sh
Here's a launchd plist that should achieve the same without any absolute paths:
<?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>Label</key>
<string>local.keep_dropbox_running</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/open</string>
<string>-W</string>
<string>-a</string>
<string>Dropbox</string>
</array>
<key>KeepAlive</key>
<true />
<key>RunAtLoad</key>
<true />
</dict>
</plist>
Terminal Tips: bash cron script to keep an app running
Oct 21st 2010 8:31PM (TUAW.com)Program
This key maps to the first argument of execvp(3). If this key is missing,
then the first element of the array of strings provided to the
ProgramArguments will be used instead.
According to the execvp manpage, a call to execvp uses the PATH environment variable if it is set. Otherwise, it searches /usr/bin and /bin. The absolute path is necessary because the directory containing the actual executable for a given application (usually /Contents/MacOS/) is unlikely to be in a user's PATH. Even if it were, it's possible that a user's PATH hasn't been set when launchd loads a given job (e.g. if .bash_profile is not read until after all launchd jobs have been loaded).
On another note, a big downside of the script above is that it cannot check the exit status of the application it is keeping alive. In other words, you cannot reliably quit an application (if you need to) without disabling the cron job.
Here are a couple of scripts that generate/install launchd jobs to relaunch a given application or executable if it exits unsuccessfully:
http://www2.uic.edu/~pgavli2/keep-running.sh
http://www2.uic.edu/~pgavli2/gen-keepalive-plist.sh
So long as an application is guaranteed to exit with a non-zero status when it crashes, these jobs should operate properly.
TUAW Retro Giveaway Part 2: Manuals, BASIC books and a printer
Apr 2nd 2009 1:49AM (TUAW.com)Pac-man + Mii = Pac-miin
Apr 4th 2007 3:21PM (Joystiq)Seriously.
University of Washington plays the most WoW?
May 26th 2006 5:28PM (Joystiq)