linux openfile参数修改

 

https://happyqing.iteye.com/blog/1953563

cat /proc/14028/limits  查看当前进程配的open files数
 

Using username "hms".

Last login: Wed Mar 27 16:06:34 2019 from 10.35.60.156

hms@linux-198x:~> su root

Password:

linux-198x:/home/hms # ulimit -a

core file size          (blocks, -c) 0

data seg size           (kbytes, -d) unlimited

scheduling priority             (-e) 0

file size               (blocks, -f) unlimited

pending signals                 (-i) 127920

max locked memory       (kbytes, -l) 64

max memory size         (kbytes, -m) unlimited

open files                      (-n) 1024

pipe size            (512 bytes, -p) 8

POSIX message queues     (bytes, -q) 819200

real-time priority              (-r) 0

stack size              (kbytes, -s) 8192

cpu time               (seconds, -t) unlimited

max user processes              (-u) 127920

virtual memory          (kbytes, -v) unlimited

file locks                      (-x) unlimited

linux-198x:/home/hms # cd /etc/se

security/  selinux/   sensors.d/

linux-198x:/home/hms # cd /etc/security/

linux-198x:/etc/security # ls

access.conf  group.conf  limits.conf  namespace.conf  namespace.init  pam_env.conf  pam_mount.conf.xml  pam_winbind.conf  pwquality.conf  sepermit.conf  time.conf

linux-198x:/etc/security # vi limits.conf

linux-198x:/etc/security # vi limits.conf

linux-198x:/etc/security # bash

linux-198x:/etc/security # ulimit -a

core file size          (blocks, -c) 0

data seg size           (kbytes, -d) unlimited

scheduling priority             (-e) 0

file size               (blocks, -f) unlimited

pending signals                 (-i) 127920

max locked memory       (kbytes, -l) 64

max memory size         (kbytes, -m) unlimited

open files                      (-n) 1024

pipe size            (512 bytes, -p) 8

POSIX message queues     (bytes, -q) 819200

real-time priority              (-r) 0

stack size              (kbytes, -s) 8192

cpu time               (seconds, -t) unlimited

max user processes              (-u) 127920

virtual memory          (kbytes, -v) unlimited

file locks                      (-x) unlimited

linux-198x:/etc/security #

 

 

概要:
linux系统默认open files数目为1024, 有时应用程序会报Too many open files的错误,是因为open files 数目不够。这就需要修改ulimit和file-max。特别是提供大量静态文件访问的web服务器,缓存服务器(如squid), 更要注意这个问题。
网上的教程,都只是简单说明要如何设置ulimit和file-max, 但这两者之间的关系差别,并没有仔细说明。

说明:
1. file-max的含义。man proc,可得到file-max的描述:
/proc/sys/fs/file-max
This file defines a system-wide limit on the number of open files for all processes. (See
also setrlimit(2), which can be used by a process to set the per-process limit,
RLIMIT_NOFILE, on the number of files it may open.) If you get lots of error messages
about running out of file handles, try increasing this value:
即file-max是设置 系统所有进程一共可以打开的文件数量 。同时一些程序可以通过setrlimit调用,设置每个进程的限制。如果得到大量使用完文件句柄的错误信息,是应该增加这个值。
也就是说,这项参数是系统级别的。

2. ulimit
Provides control over the resources available to the shell and to processes started by it, on systems that allow such control.
即设置当前shell以及由它启动的进程的资源限制。
显然,对服务器来说,file-max, ulimit都需要设置,否则就可能出现文件描述符用尽的问题

修改:
1.修改file-max

# echo  6553560 > /proc/sys/fs/file-max  //sysctl -w "fs.file-max=34166",前面2种重启机器后会恢复为默认值
或
# vim /etc/sysctl.conf, 加入以下内容,重启生效
fs.file-max = 6553560

 
2.修改ulimit的open file,系统默认的ulimit对文件打开数量的限制是1024

# ulimit -HSn 102400  //这只是在当前终端有效,退出之后,open files又变为默认值。当然也可以写到/etc/profile中,因为每次登录终端时,都会自动执行/etc/profile
或
# vim /etc/security/limits.conf  //加入以下配置,重启即可生效
* soft nofile 65535 
* hard nofile 65535

 
附录:
附录1.
为了让一个程序的open files数目扩大,可以在启动脚本前面加上ulimit -HSn 102400命令。但当程序是一个daemon时,可能这种方法无效,因为没有终端。

附录2.
如果某项服务已经启动,再动态调整ulimit是无效的,特别是涉及到线上业务就更麻烦了。
这时,可以考虑通过修改/proc/’程序pid’/limits来实现动态修改!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值