MacOS – How to make ”sudo launchctl limit’ change permanent

launchdmacosmojave

I have a background job that failed due to too many open files error.

To fix it, I have to kill the job, then run the following

sudo launchctl limit maxfiles 10240 10240

before restarting it.

Is there any way I can make the launchctl limit maxfiles change permanent?

Best Answer

You can create a launch daemon to run that command on every startup. Create /Library/LaunchDaemons/private.maxfiles.plist with the following contents:

<?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>private.maxfiles</string>
        <key>ProgramArguments</key>
        <array>
            <string>launchctl</string>
            <string>limit</string>
            <string>maxfiles</string>
            <string>10240</string>
            <string>10240</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
    </dict>
</plist>