Create a gist now

Instantly share code, notes, and snippets.

@abernix /ulimit.md Secret
Last active Sep 20, 2017

What would you like to do?

Raise Open File Limits in OS X

in OS X 10.4 to macOS sierra 10.12 and maybe higher!

Create Launcher Script:

/Library/LaunchDaemons/limit.maxfiles.plist

Copy this entire code block and paste it into your terminal and push Return to create this file for you with correct permissions. It will (probably) ask for your password:

cat <<EOM|sudo tee /Library/LaunchDaemons/limit.maxfiles.plist > /dev/null 2>&1
<?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>524288</string>
       <string>524288</string>
     </array>
   <key>RunAtLoad</key>
     <true />
 </dict>
</plist>
EOM

Activating

To activate this launcher without rebooting, run:

$ sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist

Confirmation

There are three different things you should check (thus adding to the overall confusion of this process)

  1. launchctl limit maxfiles

    $ launchctl limit maxfiles
      maxfiles    524288         524288

    Both values should be 524288 (these are the 'soft' and 'hard' limits)

  2. sysctl kern.maxfiles kern.maxfilesperproc

    $ sysctl kern.maxfiles kern.maxfilesperproc
    kern.maxfiles: 524288
    kern.maxfilesperproc: 524288

    Again, both should be 524288!

  3. ulimit -n

    You must open a new terminal for this!

    $ ulimit -n
    524288

    The value returned should be 524288! If it's not, proceed to "Troubleshooting" below. If it is 524288, then you're all done!

Troubleshooting

Permissions

Permissions on the /Library/LaunchDaemons/limit.maxfiles.plist file are very important. If you used these instructions, they should be correct by default, but make sure it is owned by user root and group wheel and umask 0644 (i.e. -rw-r--r-- root:wheel when you do ls -l on it)

$ sudo chmod 0644 /Library/LaunchDaemons/limit.maxfiles.plist
$ sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist

Cleanup Old Configurations

There has never been an officially recommended way of doing this and Apple has randomly changed the way it could be done throughout the years. If you're still finding that the ulimit -n is not returning the proper value, you should check some other places where it may be misconfigured. This should fix the problem for you, but sometimes old files may be getting in the way.

Make sure to remove any other sysctl commands involving maxfiles or maxfilesperproc and any ulimit -n <blah> commands that you may have in:

  • /etc/sysctl.conf
  • /etc/launchd.conf
  • /etc/profile

Additionally, check your shell profile settings:

Bash

  • /Users/YOUR_USERNAME/.bash_profile
  • /Users/YOUR_USERNAME/.bashrc

Zsh

  • /Users/YOUR_USERNAME/.zshrc
  • /Users/YOUR_USERNAME/.zshenv

In General

  • /Users/YOUR_USERNAME/.profile

This is excellent - thanks for putting this together!

Owner

abernix commented Nov 21, 2016

Thanks, @hwillson! I hope to eventually propose incorporating the 'troubleshooting' into automated checks when this line fails in meteor.

This document came as a result of me potentially being too authoritative with this not-super-tested and currently-abandoned script which was meant to do this all automatically for a user who I thought might be affected by it. They figured it out before I had them test it.

Sadly, Apple doesn't really have a "right" way documented to do this process, which has probably led to so many different recommended approaches. I like this LaunchDaemon approach most because:

  • It's at least a somewhat recommended way of changing the system by Apple.
  • Is at least somehow recognizable by other tools (sudo launchctl list, and some GUI tools).

MiMaMuh commented Nov 22, 2016

@abernix Thx, saved my day!

emenoh commented Jan 26, 2017

Had to restart for ulimit -n to report correct value

Excellent and very well documented solution - thanks!

Owner

abernix commented Feb 16, 2017 edited

@emenoh Thanks for reporting that peculiarity. I found that a new shell was sufficient, but absolutely mandatory as ulimit settings are set (once) when the shell parent process is created by the kernel and can only be lowered after that. Anyhow, glad the reboot still fixed it for you!

👍 on reboot for ulimit -n to report correctly, thanks @emenoh!

+1 for reboot for ulimit -n to report correctly. This should be added to the guide.

And thx @abernix for creating this guide. Just ran into that issue with Meteor 1.5 for the first time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment