如何增加 macOS Big Sur 中的最大打开文件数?

Hap*_*ace 7 kernel launchctl ulimit macos macos-bigsur

我听说

sudo launchctl limit maxfiles 64000 unlimited
Run Code Online (Sandbox Code Playgroud)

conf=/etc/sysctl.conf
if sudo cat $conf | command rg kern.maxfiles ; then
  ecerr "kern.maxfiles is already set in $conf"
else
  sudo echo 'kern.maxfiles=40480
kern.maxfilesperproc=28000' >> "$conf"
fi
Run Code Online (Sandbox Code Playgroud)

对于以前版本的 macOS,但这些都不适用于 Big Sur。

小智 10

Big Sur 不再支持 /etc/sysctl.conf。要进行持久更改,可以使用 /Library/LaunchDaemons/com.startup.sysctl.plist 中的启动守护程序在启动时发出 systctl

<?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>com.startup.sysctl</string>
    <key>LaunchOnlyOnce</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/sbin/sysctl</string>
        <string>kern.maxfiles=40480</string>
        <string>kern.maxfilesperproc=28000</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)

并安装它

chown root:wheel /Library/LaunchDaemons/com.startup.sysctl.plist
launchctl load /Library/LaunchDaemons/com.startup.sysctl.plist
Run Code Online (Sandbox Code Playgroud)


小智 7

我在 Big Sur v11.6 上成功使用了以下技术

所以看来这里有多种设置在起作用。

每个进程的软限制和硬限制返回ulimit -Sn的数字。ulimit -Hn

第一个返回的数字launchctl limit maxfiles是每个进程的软限制,第二个是每个进程的硬限制。

然后其结果sysctl -a | grep maxfiles显示kern.maxfilesand kern.maxfilesperprocwherekern.maxfiles应该是所有进程的硬限制,并且kern.maxfilesperproc应该是单个进程的硬限制。

看来我可以通过添加文件来永久设置所有这些/Library/LaunchDaemons/limit.maxfiles.plist

内容如下,其中 524288 对应软限制,16777216 对应硬限制。

<?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>16777216</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>ServiceIPC</key>
    <false/>
  </dict>
</plist>
Run Code Online (Sandbox Code Playgroud)

为了使该文件生效,您需要确保该文件由 root 拥有并具有组轮。

sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist
Run Code Online (Sandbox Code Playgroud)

您还需要加载守护进程。

sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist
Run Code Online (Sandbox Code Playgroud)

最后,为了影响输出ulimit -Snulimit -Hn我需要重新启动我的机器。

注意:通过 launchctl 设置每个进程的软和硬限制似乎是一个错误,会影响内核最大每个进程和最大文件限制,但这就是发生的情况,所以我只是选择了足够大的值,我认为它不太可能会遇到任何麻烦。


小智 1

尝试:

sudo sysctl kern.maxfiles=64000 kern.maxfilesperproc=28000
Run Code Online (Sandbox Code Playgroud)

不幸的是,它不会重新启动后持续存在。将这些添加到 /etc/sysctl.conf 似乎在 Big Sur 中不起作用。

要查看您当前的设置,请使用:

sysctl -a | grep maxfiles
Run Code Online (Sandbox Code Playgroud)