If you encounter an Too many open files
in Swoole's log, don't panic. In the process of developing TCP network applications, you often encounter the problem of Too many open files
This shows that your program has reached the maximum number of open files allowed by Linux. Need to modify ulimit
settings
You can use lsof
view the number of file handles opened by the process:
# 将 PID 修改为你要检查的进程ID
lsof -p PID | wc -l
You can also remove | wc -l
to check the file handles opened by the service, but not closed.
You can use ulimit -n
see how much the current setting is
[root@shenyan ~]# ulimit -n
100001
If it is too small, it needs to be adjusted. The Swoole document recommends that ulimit -n
should be adjusted to 100000
or even larger.
So how to modify it?
ulimit -n 100000
can modify it by executing 06128beb7ec04b under the command line.
However, it should be noted that sometimes this modification method is only valid for the current terminal. Closing or reopening a terminal will restore the previous settings.
At this time, you need to modify /etc/security/limits.conf
and add
* soft nofile 262140
* hard nofile 262140
root soft nofile 262140
root hard nofile 262140
* soft core unlimited
* hard core unlimited
root soft core unlimited
root hard core unlimited
After modifying the limits.conf
file, you need to restart the system to take effect.
After this operation is completed, if the error Too many open files
is still reported, then you can try to check the running process limit:
# 将 PID 修改为你要检查的进程ID
cat /proc/PID/limits
If the Max open files
here is too small, it also needs to be modified.
Most occur in this case the use of supervisor
tools such as time management, supervisor
start the service default minfds
configuration is 1024
, so there will be Too many open files
.
The same is true when systemd
LimitNOFILE
needs to be modified. If not set or set to LimitNOFILE=unlimited
(not recognized), the default is 1024
.
And here also pay attention to setting LimitNOFILE=infinity
be equal to LimitNOFILE=65536
. For those who need more than 100,000 file openings, you must set it yourself.
In summary, the solution when Too many open files
ulimit -n
supervisor
:minfds
systemd
:LimitNOFILE
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。