linux - supervisor管理的服务,出现了too many open files
前言
linux 系统对文件句柄数量是有限制的。一般是系统限制、用户限制和程序限制。
/proc/sys/fs/file-max
这个文件保存着当前系统的文件句柄的个数。这个是系统级别的限制,针对的是整个系统,并不是针对用户。
这个参数可以通过 sysctl.conf 进行配置:
fs.file-max = 1601172
/etc/security/limits.conf
限制一个用户的所有 shell 能打开的最大数。
* soft nofile 65535
* hard nofile 65535
修改以后,需要重新登录终端才能生效。
ulimit  -n
控制进程级别能够打开的文件句柄的数量。提供对 shell 及其启动的进程的可用文件句柄的控制。这是进程级别的。
查看已经启动程序的文件句柄限制
cat /proc/[pid]/limits
Limit                     Soft Limit           Hard Limit           Units
Max cpu time              unlimited            unlimited            seconds
Max file size             unlimited            unlimited            bytes
Max data size             unlimited            unlimited            bytes
Max stack size            8388608              unlimited            bytes
Max core file size        unlimited            unlimited            bytes
Max resident set          unlimited            unlimited            bytes
Max processes             62717                62717                processes
Max open files            1048576              1048576              files
Max locked memory         16777216             16777216             bytes
Max address space         unlimited            unlimited            bytes
Max file locks            unlimited            unlimited            locks
Max pending signals       62717                62717                signals
Max msgqueue size         819200               819200               bytes
Max nice priority         0                    0
Max realtime priority     0                    0
Max realtime timeout      unlimited            unlimited            us
supervisor 控制的程序
CentOS 上使用系统自带的 supervisor,使用 systemd 启动 supervisord 的服务。被 supervisor 管理的程序,继承的是 systemd 对应的限制,如果需要修改的话,就需要在启动.service 文件里面修改对应的限制
vim  /usr/lib/systemd/system/supervisord.service
[Unit]
Description=Process Monitoring and Control Daemon
After=network.target
[Service]
Type=forking
LimitNOFILE=40960
LimitNPROC=40960
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
ExecReload=/usr/bin/supervisorctl reload
ExecStop=/usr/bin/supervisorctl shutdown
[Install]
WantedBy=multi-user.target
总结
现在很多的 golang 程序也是直接使用 systemd 管理服务,所以也会涉及到文件句柄的限制,也是同样的解决方法。
- 原文作者:Linux运维菜
- 原文链接:https://www.opcai.top/post/2018/2018-12/centos_supervisor_err/
- 版权声明:本作品采用进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。