Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

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.
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.