MacOS – Automatically relaunch a closed application

launchdmacos

I have an application that opens when I login.

Is there a way to relaunch it automatically if it crashes or if I close it inadvertently?
(the application in question is Transmission if there is any solution specific to this app)

In a perfect world, it could even be launched even without me opening a session.

thanks.

Best Answer

Here is my launchd script to keep SomeApp always running. It is in ~/Library/LaunchAgents/ and called SomeApp.restart.plist

<?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>
</dict>
</plist>

Load it once with

launchctl load ~/Library/LaunchAgents/SomeApp.restart.plist

Launchctl will run this after reboots.

  • RunAtLoad will launch the application the first time launchctl runs this
  • KeepAlive will restart it if the application quits (CMD+Q or crash)

Should run forever. If you want a GUI tool to help, Lingon works even though development has stopped.