16

I have an application that opens when I log in.

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.

1

3 Answers 3

29

Here is my launchd script to keep SomeApp always running.

Name it SomeApp.restart.plist and place it 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>
</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.

4
  • 1
    Lingon and launchd go together like peanut butter and chocolate, only better.
    – bmike
    Jul 14, 2011 at 19:42
  • Lingon 3 is in the App Store now as well, but it's seriously crippled. v2.1.1 at sourceforge.net/projects/lingon/files/Lingon/2.1.1 is way better.
    – neu242
    Sep 3, 2012 at 7:25
  • Beautiful! Quick note, spaces do NOT have to be escaped! So, if the app was called "Some App.app", the line would be: " <string>/path/to/Some App.app/Contents/MacOS/Some App</string>". Dec 27, 2018 at 22:44
  • @neu242 That version doesn't seem to work with the newest MacOS anymore. There are paid versions Lingon 3 and Lingon X though.
    – xji
    May 21, 2020 at 21:58
8

You could write a launchd script to do it. Launchd can watch for applications and restart them if they disappear. I might have one somewhere if you are interested.

Updated with an example...

See mankoff's example. Here's mine, it's slightly different but works the same...

<?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>keep.it.running</string>  
    <key>Program</key>  
    <string>/Applications/Address Book.app/Contents/MacOS/Address Book</string>  
</dict>  
</plist>  
3
  • This is something i can explore. Could you please send me an example? thx
    – Pierre
    Aug 31, 2010 at 11:42
  • Deleted my comment because i couldn't format the code.
    – Henry
    Sep 1, 2010 at 1:05
  • Sorry I accepted the other answer because the script seemed more complete and there was the reference to Lingon. But you were the first to provide the right answer, so thanks for that ;)
    – Pierre
    Sep 1, 2010 at 9:23
0

You can open applications on the background with open -jg or the launch command in AppleScript.

#!/bin/bash

open -jg -a FastScripts -a Mail
osascript -e 'launch app "Alfred"'

KeepAlive is not needed. RunAtLoad would also run the program on the zeroth interval.

<?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>com.stackexchange.apple.837</string>
    <key>Program</key>
    <string>/Users/username/Library/Scripts/open applications.sh</string>
    <key>StartInterval</key>
    <integer>1</integer>
</dict>
</plist>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .