Linux 工作过程的“最大打开文件”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3734932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:34:14  来源:igfitidea点击:

"Max open files" for working process

linuxfileprocesslimit

提问by wako

Is it possible to increase "Max open files" parameter for working process ? I mean this parameter:

是否可以为工作过程增加“最大打开文件”参数?我的意思是这个参数:

cat /proc/<pid>/limits | grep files

Thanks for your advices

谢谢你的建议

采纳答案by Brian Agnew

As a system administrator: The /etc/security/limits.conffile controls this on most Linux installations; it allows you to set per-user limits. You'll want a line like myuser - nofile 1000.

作为系统管理员:该/etc/security/limits.conf文件控制着大多数 Linux 安装;它允许您设置每个用户的限制。你会想要像myuser - nofile 1000.

Within a process: The getrlimit and setrlimit callscontrol most per-process resource allocation limits. RLIMIT_NOFILEcontrols the maximum number of file descriptors. You will need appropriate permissions to call it.

在进程内getrlimit 和 setrlimit 调用控制大多数每个进程的资源分配限制。RLIMIT_NOFILE控制文件描述符的最大数量。您将需要适当的权限才能调用它。

回答by Brian Agnew

This linkdetails how to change this system wideor per user.

此链接详细说明如何更改此系统范围每个用户

Many application such as Oracle database or Apache web server needs this range quite higher. So you can increase the maximum number of open files by setting a new value in kernel variable /proc/sys/fs/file-max as follows (login as the root):

$ sysctl -w fs.file-max=100000

You need to edit /etc/sysctl.conf file and put following line so that after reboot the setting will remain as it is

许多应用程序,例如 Oracle 数据库或 Apache Web 服务器,对这个范围的要求更高。因此,您可以通过在内核变量 /proc/sys/fs/file-max 中设置新值来增加最大打开文件数,如下所示(以 root 身份登录):

$ sysctl -w fs.file-max=100000

您需要编辑 /etc/sysctl.conf 文件并输入以下行,以便重新启动后设置将保持原样

回答by lornix

You could use gdb, break into the process, call the aforementioned syscalls to raise the limit you're interested in, then continue the job and exit gdb. I've edited things on the fly this way a few times.

您可以使用 gdb,闯入进程,调用上述系统调用来提高您感兴趣的限制,然后继续工作并退出 gdb。我已经多次以这种方式即时编辑内容。

Your app wouldn't be down, but just frozen for the moment while you performed the call. if you're quick (or you script it!), it'll likely not be noticeable.

您的应用程序不会关闭,而是在您执行呼叫时暂时冻结。如果您很快(或者您编写脚本!),它可能不会引起注意。

回答by Gaurav KS

Yes, it is possible to increase the limits in /proc/<pid>/limitsat run time. Just find the pid and execute below command:

是的,可以/proc/<pid>/limits在运行时增加限制。只需找到pid并执行以下命令:

echo -n "Max open files=20:20" > /proc/$pid/limits

回答by Stephen P. Schaefer

echo -n "Max open files=20:20" > /proc/$pid/limits

...works in RHEL5.5 and RHEL6.7.

...适用于 RHEL5.5 和 RHEL6.7。

Note that the "-n" is mandatory; a trailing newline will generate a complaint about invalid arguments.

请注意,“-n”是强制性的;尾随的换行符将产生关于无效参数的投诉。

回答by rkachach

Another option is to use the prlimit command (from the util-linux package). For example if you want to set the maximum number of open files for a running process to 4096:

另一种选择是使用 prlimit 命令(来自 util-linux 包)。例如,如果要将正在运行的进程的最大打开文件数设置为 4096:

prlimit -n4096 -p pid_of_process

prlimit -n4096 -p pid_of_process

回答by Ravi

The following commands give the max # of open files per process permissible by default limits (soft and hard respectively):

以下命令给出了默认限制(分别是软和硬)每个进程允许的最大打开文件数:

ulimit -Sa
ulimit -Ha

You can use a program or a command to change these limits. Look at ulimit (man ulimit).

您可以使用程序或命令来更改这些限制。看看ulimit(man ulimit)。

回答by Nick Woodhams

On Ubuntu 16.04, with a rethinkdb process running, none of these solutions worked.

在 Ubuntu 16.04 上,运行 rethinkdb 进程时,这些解决方案均无效。

I kept getting error: accept() failed: Too many open files.

我不断得到 error: accept() failed: Too many open files.

What ultimately worked was this in my /etc/security/limits.conffile. Note the nprocin addition to the nofile. As I understand it, root needs to specified separately.

最终有效的是我的/etc/security/limits.conf文件中的这个。注意除了 nofile 之外的nproc。据我了解,root 需要单独指定。

*                soft    nofile          200000
*                hard    nofile          1048576
root             soft    nofile          200000
root             hard    nofile          1048576
*                soft    nproc           200000
*                hard    nproc           1048576
root             soft    nproc           200000
root             hard    nproc           1048576

You can see the system max files by running cat /proc/sys/fs/file-max. I just set mine to a high maximum well within reason for the size of the server.

您可以通过运行查看系统最大文件cat /proc/sys/fs/file-max。我只是在服务器大小的合理范围内将我的设置设置为一个很高的最大值。

You can verify the max open files your process is allowed by running cat /proc/{your-pid}/limits.

您可以通过运行来验证您的进程允许的最大打开文件数cat /proc/{your-pid}/limits

Helpful post: https://medium.com/@muhammadtriwibowo/set-permanently-ulimit-n-open-files-in-ubuntu-4d61064429a

有用的帖子:https: //medium.com/@muhammadtriwibowo/set-permanently-ulimit-n-open-files-in-ubuntu-4d61064429a