MacOS – Can launchd be made to set a different owner on files it creates while executing a .plist job

launchdmacosplist

I have a launchctl .plist file in /Library/LaunchDaemons as follows:

<?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>david.ports.slocate.home.updatedb</string>
    <key>ProgramArguments</key>
    <array>
        <string>/opt/local/bin/slocate</string>
        <string>-U</string>
        <string>/Users/david</string>
        <string>-o</string>
        <string>/Users/david/slocate_david.db</string>
    </array>
    <key>Umask</key>
    <string>077</string>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>5</integer>
        <key>Minute</key>
        <integer>45</integer>
    </dict>
</dict>
</plist>

For those who are wondering, I use the slocate port from MacPorts instead of the native locate database that ships with OS X. This .plist job updates the slocate database specific to my home directory at 5:45 AM daily. The Umask setting of 077 makes the output file readable only by me. However, the file is created with the ownership of root; not what I want.

Is there a way to specify the ownership of the output file instead of the default of root? I realize I can run this as a LaunchAgent under ~/Library/LaunchAgents, but I prefer the job to run whether I am logged in or not, hence installing it as a LaunchDaemon.

I'm running OS X 10.10.3 Yosemite.

Best Answer

I have a few suggestions. According to to the manual, you can set the UserName key- https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html. You could use periodic with a shell script to run slocate and change the permissions of the database. Setup a user crontab. A user crontab will only run at the specified time.