Macos – Can’t launch daemon with launchctl in Yosemite

launchctllaunchdmacososx-yosemite

I have a launchd daemon placed in ~/Library/LaunchAgents that worked well in Mavericks. But it won’t start in Yosemite public beta. The daemon plist is like this (my username is darksair with UID 501)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd >
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>org.darksair.retrmail</string>
    <key>ProgramArguments</key>
    <array>
      <string>/Users/darksair/bin/retrmail.py</string>
    </array>
    <key>KeepAlive</key>
    <false/>
    <key>StartInterval</key>
    <integer>300</integer>
    <key>LaunchOnlyOnce</key>
    <false/>
    <key>UserName</key>
    <string>darksair</string>
    <key>ProcessType</key>
    <string>Standard</string>
    <key>EnvironmentVariables</key>
    <dict>
      <key>PATH</key>
      <string>/Users/darksair/Python/bin:/Users/darksair/Python3/bin:/Users/darksair/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
    </dict>
    <key>StandardOutPath</key>
    <string>/Users/darksair/logs/retrmail.log</string>
    <key>StandardErrorPath</key>
    <string>/Users/darksair/logs/retrmail.log</string>
  </dict>
</plist>

Basically it is supposed to run ~/bin/retrmail.py every 5 minutes.

I notice that in Yosemite launchd is upgraded to 2.0, and launchctl has new commands. I tried

sudo launchctl kickstart user/501/org.darksair.retrmail

and it said

Could not find service "org.darksair.retrmail" in domain for uid: 501

I also tried the old school

sudo launchctl load ~/Library/LaunchAgents/retrmail.plist

and it said

/Users/darksair/Library/LaunchAgents/retrmail.plist: Path had bad ownership/permissions

The file is owned by me and the staff group. I tried both permission 644 and 600 with the same error.

So does anyone know how to properly fire up a launchd daemon in Yosemite?


UPDATE: Looks like my launch agent file has to be owned by root:wheel. After I chown, I tried

sudo launchctl load ~/Library/LaunchAgents/retrmail.plist

and it didn’t issue any error. And I think my deamon is running properly. I’ll leave this question open because I remember the launchd document clearly states that the launch agent file can be owned by the user running the daemon.


UPDATE2: No it wasn’t running properly. It got run only once, but not again, as if it was unloaded.


UPDATE3: I upgraded to Yosemite public beta 3, and changed my agent to this

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd >
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>org.darksair.retrmail</string>
    <key>ProgramArguments</key>
    <array>
      <string>/Users/darksair/bin/retrmail.py</string>
    </array>
    <key>StartInterval</key>
    <integer>300</integer>
    <key>UserName</key>
    <string>darksair</string>
    <key>EnvironmentVariables</key>
    <dict>
      <key>PATH</key>
      <string>/Users/darksair/Python/bin:/Users/darksair/Python3/bin:/Users/darksair/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
    </dict>
    <key>StandardOutPath</key>
    <string>/Users/darksair/logs/retrmail.log</string>
    <key>StandardErrorPath</key>
    <string>/Users/darksair/logs/retrmail.log</string>
  </dict>
</plist>

I reloaded this agent, and I think now it is working properly. I’m still leaving this question open because I don’t know what’s wrong with my previous plist.


In conclusion, what I found is I have to change the owner of the plist to root:wheel in order to load it.

Best Answer

From man launchctl

Note that per-user configuration files (LaunchAgents) must be owned by root (if they are located in /Library/LaunchAgents) or the user loading them (if they are located in $HOME/Library/LaunchAgents). All system-wide daemons (LaunchDaemons) must be owned by root. Configuration files must disallow group and world writes. These restrictions are in place for security reasons, as allowing writability to a launchd configuration file allows one to specify which executable will be launched.

Fix is

sudo chmod 600 /Library/LaunchDaemons/x.plist
sudo chown root /Library/LaunchDaemons/x.plist