Friday, November 22, 2024

launchd – Run locate.updatedb nightly, not weekly?

The file that you tried to edit is on the read-only System volume and can’t be modified, as you discovered. But you can make the change you want by following these steps.

1. Disable the built-in job to update the locate database

This step is optional, but it will prevent the database from being updated twice in one day, once a week.

sudo launchctl disable system/com.apple.locate

To revert this step:

sudo launchctl enable system/com.apple.locate

2. Create a new job to update the database

Copy the job file to the writable LaunchDaemon folder and give it a different name:

sudo cp -p /System/Library/LaunchDaemons/com.apple.locate.plist /Library/LaunchDaemons/locate.plist

The name shown is just an example; you can use any (unique) name you want. Now edit the new file so that it reads 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>locate</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/libexec/locate.updatedb</string>
    </array>
    <key>ProcessType</key>
    <string>Background</string>
    <key>KeepAlive</key>
    <dict>
        <key>PathState</key>
        <dict>
            <key>/var/db/locate.database</key>
            <false/>
        </dict>
    </dict>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>3</integer>
        <key>Minute</key>
        <integer>15</integer>
    </dict>
    <key>AbandonProcessGroup</key>
    <true/>
</dict>
</plist>

Finally, start the new job:

sudo launchctl enable system/locate

To test the configuration, you can run the job immediately:

sudo launchctl kickstart system/locate

Related Articles

Latest Articles