Category Archives: Shell

How to persist ulimit settings in OSX

Shell Session Limit

The limits set via ulimit only affects processes created by the current shell session.

  • The “soft limit” is the actual limit that is used. It could be set, as far as it’s not greater than the “hard limit”.
  • The “hard limit” could also be set, but only to a value less than the current one, and only to a value not less than the “soft limit”.
  • The “hard limit”, as well as system-wide limits, could be raised by root (the administrator) by executing system configuration commands or modifying system configuration files.

After you terminate the shell session (by Ctrl+D, exit, or closing the Terminal.app window, etc.), the settings are gone. If you want the same setting in the next shell session, add the setting to the shell startup script.

NOTE: If you are using bash, then it should be ~/.bash_proile or ~/.bash_login. If you are using other shells, it should probably be ~/.profile.

System Limit (Requires Reboot to Take Effect)

For older Mac OS X (I guess it works on 10.7 (Lion) or before):You may add the following line to /etc/launchd.conf (owner: root:wheel, mode: 0644):

limit maxfiles 262144 524288

You should change the numbers according to your needs. They are the “soft limit” (262144) and the is “hard limit” (524288) respectively.

For 10.8 (Mountain Lion):You may add the following lines to /etc/sysctl.conf (owner: root:wheel, mode: 0644):

kern.maxfiles=524288
kern.maxfilesperproc=262144

You should change the numbers according to your needs. They are the “system-wide limit” (kern.maxfiles) and “per-process limit” (kern.maxfilesperproc) respectively. For more settings, consult the manual page by running man sysctl, or read the source code at /usr/include/sys/sysctl.h.

For 10.9 (Mavericks), 10.10 (Yosemite),  10.11 (El Capitan), and 10.12 (Sierra):You have to create a file at /Library/LaunchDaemons/limit.maxfiles.plist (owner: root:wheel, mode: 0644):

<?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>limit.maxfiles</string>
    <key>ProgramArguments</key>
    <array>
      <string>launchctl</string>
      <string>limit</string>
      <string>maxfiles</string>
      <string>262144</string>
      <string>524288</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>ServiceIPC</key>
    <false/>
  </dict>
</plist>

You should change the numbers according to your needs. They are the “soft limit” (262144) and the is “hard limit” (524288) respectively. For more information, consult the manual page by running man launchd.plist.

If the system doesn’t let you set the limits above a certain value…The system doesn’t let you set a value higher than a “hard maximum” (proposed by Apple). To increase this “hard maximum”, you have to purchase “OS X Server” from “App Store”, then you have to execute the following command once:

sudo serverinfo --setperfmode true

This activates “server performance mode” on your machine. You can then set the maximum according to the configuration of your machine (see this).

References