Whats the best way to automatically start an app if its not running?

I want to automatically start an application if its not running. My use is case that if I quit a particular application I want to automatically restart. Specifically in my case its the Messages app.

I always want to keep the Messages app running so if I quit Messages I want it to restart.

What's the best way to handle this?


What you are looking for is launchd.

It will allow you to run an app continuously (auto-start at boot, relaunch when it quits/crash)

To use it add a plist file in ~/Library/LaunchAgents/

<?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>RunAtLoad</key>  
        <true/>
        <key>KeepAlive</key>
        <true/>
        <key>Label</key>
        <string>SomeApp.restart</string>
        <key>ProgramArguments</key>
        <array>
                <string>/path/to/SomeApp.app/Contents/MacOS/SomeApp</string>
        </array>
        <key>StartCalendarInterval</key>
        <dict>
            <key>Hour</key>
            <integer>5</integer>
            <key>Minute</key>
            <integer>10</integer>
       </dict>
</dict>
</plist>

Then load it once with launchctl load ~/Library/LaunchAgents/SomeApp.restart.plist

  • The RunAtLoad option will launch the application the first time launchctl runs this.
  • The KeepAlive option will re-launch the application if it crashes.
  • The StartCalendarInterval will run it at a certain time. Presumably this is mutually exclusive with the previous two, but is included to show what can be done with launchd Launchctl will run this after reboots.

As mentioned, launchctl is the command you want. I find that the syntax for launchctl to be rather difficult compared to crontab. Of course, launchctl does much more than cron. Thus, I use Lingon to provide a GUI to manage launchctl.