Search code examples
macosdaemonosx-elcapitanlaunchd

launchd does not 'keep alive' processes in El Capitan


I'm trying to make sure a process is always running, even after it quits, crashes or stops in anyway. It's a small binary that reads a serial line and writes to a database - nothing too complex. If it fails to read, it quits with exitcode 70 and it captures any SIGKILL or SIGTERM events and shuts down it's database connections gracefully before actually quitting.

However, the process does NOT launch at load (even though this flag is set), nor does it restart if it is killed. Here is the 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>Disabled</key>
    <false/>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>blah.bloop.tag05</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/blah/Desktop/rfid</string>
        <string>-f/dev/tty.usbserial-FT32X30YBXB</string>
        <string>-n5</string>
        <string>-ctcp://127.0.01</string>
        <string>-v</string>
        <string>-x100000</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/var/log/rfid.log</string>
    <key>StandardOutPath</key>
    <string>/var/log/rfid.log</string>
    <key>WorkingDirectory</key>
    <string>/Users/blah/Desktop</string>
</dict>
</plist>

This plist lives in ~/Library/LaunchAgents (and the user in question can stop and start this process easily enough).

Any thoughts at all? I know there are other processes that are being restarted but I can't for the life of me figure out this one. I thought permissions might be it but these all seem fine :/

I decided to try another plist to see if it was my daemon program at fault. Turns out, it's launchd:

So I decided to run a quick test, using the program tail. I wanted to see if it was my daemon process itself or something to do with launchd. It seems that launchd is the problem. Here is an alternative and simple 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>KeepAlive</key>
        <true/>
        <key>Label</key>
        <string>test.test</string>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/bin/tail</string>
                <string>-f</string>
                <string>/var/log/system.log</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StandardErrorPath</key>
        <string>/tmp/test.test.err</string>
        <key>StandardOutPath</key>
        <string>/tmp/test.test.out</string>
</dict>
</plist>

I launch this process using the following command

launchctl load test.plist
launchctl start test.test

I then kill the process from another terminal by sending either SIGKILL or SIGTERM to the process with the kill command. Launchd fails to restart the process.

I suspect there must be something new in El-Capitan that I've missed?


Solution

  • Further experimentation has revealed an answer. It appears that only Daemons, such as 'Global Daemons' respect the restart and RunAtLaunch directives. I notice that there are a couple of plists in /Library/LaunchDaemons such as these for TeamViewer and other third party programs.

    Interestingly, one can send SIGKILL to processes belonging to Apple such as ImageCaptureAgent and the KeepAlive directive is NOT respected. This is an Apple defined agent plist file that explicitly states a process should be restarted if it is killed, but it does not.

    I wonder if Apple changed this functionality in the newer release?