MacOS – launchd UserName key not working

launchdmacosterminal

So I am trying to write a launchd file to run a script on a Mac Mini as a specific user when they are not logged in. This is the plist file:

<?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.wintr.eodemail</string>
    <key>Program</key>
    <string>/Users/*myusername*/Desktop/testdaemon/testdaemon.sh</string>
    <key>StandardErrorPath</key>
    <string>/var/log/eod-email.log</string>
    <key>StandardOutPath</key>
    <string>/var/log/eod-email.log</string>
    <key>RunAtLoad</key>
    <true/>
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Minute</key>
            <integer>20</integer>
        </dict>
    </array>
</dict>
</plist>

I have this placed in /Library/LaunchDaemons/ and it works fine running as the root user. Runs exactly when I tell it when no one is logged on, however when I add the UserName key it suddenly stops working:

<?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.wintr.eodemail</string>
    <key>Program</key>
    <string>/Users/*myusername*/Desktop/testdaemon/testdaemon.sh</string>
    <key>StandardErrorPath</key>
    <string>/var/log/eod-email.log</string>
    <key>StandardOutPath</key>
    <string>/var/log/eod-email.log</string>
    <key>RunAtLoad</key>
    <true/>
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Minute</key>
            <integer>20</integer>
        </dict>
    </array>
    <key>UserName</key>
    <string>*myusername*</string>
</dict>
</plist>

When I unload and load the file, then check if it loaded correctly with sudo launchctl list | grep wintr it shows:

-       78      com.wintr.eodemail

And doesn't run. What am I doing wrong here?

Best Answer

As fd0 pointed out, my user could not write to the directory /private/var/log, since I did not need the logs I removed the following lines:

<key>StandardErrorPath</key>
<string>/var/log/eod-email.log</string>
<key>StandardOutPath</key>
<string>/var/log/eod-email.log</string>

And it started working! I could have also changed these to a directory my user can write to to fix the issue.