Skip to content

Instantly share code, notes, and snippets.

@abernix
Last active February 4, 2024 23:05
Show Gist options
  • Star 127 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abernix/a7619b07b687bb97ab573b0dc30928a0 to your computer and use it in GitHub Desktop.
Save abernix/a7619b07b687bb97ab573b0dc30928a0 to your computer and use it in GitHub Desktop.

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:

  1. Run the following command

    $ sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist
  2. At least log out of your current shell and open a new one

  3. On the more extreme side of things, close and re-open your terminal application.

  4. At the very most, restart your computer if you still haven't had luck. 😉

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. You must open a new terminal for this!: ulimit -n

    $ 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
@hwillson
Copy link

This is excellent - thanks for putting this together!

@abernix
Copy link
Author

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
Copy link

mimamuh commented Nov 22, 2016

@abernix Thx, saved my day!

@emenoh
Copy link

emenoh commented Jan 26, 2017

Had to restart for ulimit -n to report correct value

@zeroasterisk
Copy link

Excellent and very well documented solution - thanks!

@abernix
Copy link
Author

abernix commented Feb 16, 2017

@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!

@timfulmer
Copy link

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

@derwaldgeist
Copy link

+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.

@daisy-ycguo
Copy link

Thanks.
+1 for reboot for ulimit -n.

@sublimator
Copy link

Just restart the terminal process, no need for reboot

@psanders
Copy link

psanders commented Jul 8, 2019

+1 for just restarting the terminal process

@SavageWilliam
Copy link

+1 restart. Worth adding to instructions

@abernix
Copy link
Author

abernix commented Dec 11, 2019

I'm confused what's missing from the original instructions here and I'm pretty sure it's only the comments which are suggesting a reboot was necessary. The original instructions I wrote didn't suggest restarting, but instead provided the instruction to run sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist to avoid rebooting in order to fire up the launcher and, further, boldly noted that a new terminal should be opened before running ulimit -n (in the verification step).

Happy to adjust the instructions, but where specifically would you all appreciate the clarity?

@abernix
Copy link
Author

abernix commented Dec 11, 2019

I made one small edit to hoist the bold warning up higher and more prominent, but let me know what you think the instructions need cc @SavageWilliam.

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