修改操作系统句柄数和用户进程数

对于一般的应用来说(像Apache、系统进程)1024完全足够使用。但是像squid、mysql、java等单进程处理大量请求的应用来说就有点捉襟见肘了。如果单个进程打开的文件句柄数量超过了系统定义的值,就会提到“too many files open”的错误提示。怎么查看当前进程打开了多少个文件句柄呢?

lsof -n |awk ‘{print $2}‘|sort|uniq -c |sort -nr|more

在系统访问高峰时间以root用户执行上面的脚本,可能出现的结果如下:
# lsof -n|awk ‘{print $2}‘|sort|uniq -c |sort -nr|more
    131 24204
     57 24244
     57 24231
     56 24264
其中第一行是打开的文件句柄数量,第二行是进程号;得到进程号后,通过ps命令得到进程的详细内容。
ps -aef|grep 24204
mysql    24204 24162 99 16:15 ?        00:24:25 /usr/sbin/mysqld
原来是mysql进程打开最多文件句柄数量。但是他目前只打开了131个文件句柄数量,远远底于系统默认值1024;

  

//使用ulimit查看系统当前参数设置

[[email protected] ~]# ulimit  -a
core file size          (blocks, -c) 0     core文件的最大值为100 blocks
data seg size           (kbytes, -d) unlimited  进程的数据段可以任意大
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited   文件可以任意大
pending signals                 (-i) 7424
max locked memory       (kbytes, -l) 64  个任务锁住的物理内存的最大值为32kB
max memory size         (kbytes, -m) unlimited 一个任务的常驻物理内存的最大值
open files                      (-n) 1024 一个任务最多可以同时打开1024的文件
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1024 当前用户同时打开的进程(包括线程)的最大个数
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

  

1.修改用户句柄数:

vi /etc/security/limits.conf在文件末尾增加

# add open files
*           soft  core   unlimited
*           hard  core   unlimited
*           soft  fsize  unlimited
*           hard  fsize  unlimited
*           soft  data   unlimited
*           hard  data   unlimited
*           soft  nproc  65535
*           hard  nproc  63535
*           soft  stack  unlimited
*           hard  stack  unlimited
*           soft  nofile 65535
*           hard  nofile 65535

  

2.修改用户进程数:

vim /etc/security/limits.d/90-nproc.conf
把1024修改成65535,默认情况下普通用户是1024,root没有限制;

*          soft    nproc     65536
root       soft    nproc     unlimited

3.通常/etc/sysctl.conf 不需要修改,配置内存在使用到95%时启用swap具体如下:

# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled.  See sysctl(8) and
# sysctl.conf(5) for more details.

# Controls IP packet forwarding
net.ipv4.ip_forward = 0

# Controls source route verification
net.ipv4.conf.default.rp_filter = 1

# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0

# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0

# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1

# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1

# Disable netfilter on bridges.
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0

# Controls the default maxmimum size of a mesage queue
kernel.msgmnb = 65536

# Controls the maximum size of a message, in bytes
kernel.msgmax = 65536

# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736

# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296

vm.swappiness=5

  

最是执行文件生效命令:sysctl -p

  

原文地址:https://www.cnblogs.com/Alexr/p/9360573.html

时间: 2024-07-31 00:28:43

修改操作系统句柄数和用户进程数的相关文章

[Linux] 关于Centos6中ulimit nproc用户进程数的限制

一.缘由: 在启动mongodb的时候,有Warning提示soft rlimits too low,就是用户使用进程数过小,遂调高系统资源关于用户最大进程数的限制ulimit -u. 先暂时使设置生效,ulimit -u 102400,提示: ulimit: max user processes: cannot modify limit: Operation not permitted. 修改/etc/security/limits.conf 为 * soft nproc 102400,退出s

linux上限制用户进程数、cpu占用率、内存使用率

限制进程CPU占用率的问题,给出了一个shell脚本代码如下: renice +10 `ps aux | awk '{ if ($3 > 0.8 && id -u $1 > 500) print $2}'` 其中用到ps获取进程信息,其实 ps中%CPU一列的意义是进程实际占有CPU时间和它存活时间的比值,这个值能反应进程对CPU的消耗,但不能准确反应进程所占CPU时间占整个系统CPU的百分比. 而top输出中的%CPU这一列正是进程所占CPU时间占整个系统CPU的百分比,用于

记一次Oracle session数过多引起进程数超过processes限制最终导致客户端无法连接的问题

RAC突然告警,客户端尝试连接时会报:ORA-12520: TNS:listener could not find available handler for requested type of server 错误. 现整理解决过程,以便于后续遇到此问题时快速处理. 1. 登入服务器,使用sqlplus / as sysdba 发现可以进入数据库. 2. 查看alert日志,发现日志中有个错误信息:ORA-00020: maximum number of processes (2000) exc

Linux中最大进程数和最大文件数

前言 Linux系统中可以设置关于资源的使用限制,比如:进程数量,文件句柄数,连接数等等. 在日常的工作中应该遇到过: -bash: fork: retry: Resource temporarily unavailable 或者 too many open files 这些类似的操作错误,前者是由于当前用户的进程数超出限制,后者由于当前用户的文件打开数超出限制. 下面就简单的介绍下配置用户进程数和文件打开数. 一.ulimit命令 Provides control over the resou

修改linux用户的可以打开的最大文件数和进程数(fork: retry: Resource temporarily unavailable)

今天,jenkins发布应用时,遇到一个报错,如下: /etc/profile:fork: retry: Resource temporarily unavailable Google了一下,网上大家大部分说原因可能是,用户的进程数或者文件打开数,达到了限制.按照这个思路,我去查问题,发现用户的进程数为200多,文件打开数为1300多 而通过ulimit–a,看到 openfiles                      (-n) 1024 maxuser processes        

nginx优化,隐藏版本号,修改用户和组,网页缓存时间,更改进程数,以及防盗链

设置nginx优化隐藏版本号,修改用户和组,网页缓存时间,更改进程数, 以及防盗链的代码内容 ========================隐藏版本号,修改用户和组 方法一: cd /opt/nginx-1.12.0/src/core vim nginx.h #define NGINX_VERSION "1.12.0" //在编译安装前去篡改版本号达到隐藏的效果 方法二: vim /usr/local/nginx/conf/nginx.conf server_tokens off;

Linux 用户打开进程数的调整

Linux 用户打开进程数的调整 参考文章: 关于RHEL6中ulimit的nproc限制(http://www.cnblogs.com/kumulinux/archive/2012/12/16/2820609.html) !! 本文内容仅在 RHEL6.4 上测试通过.本文仅针对所有用户 1 查看nproc(max user processes)命令 [[email protected] ~]# ulimit -u 14866 2 修改nproc 临时修改, 重登录或重启后失效: [[emai

Linux进程数,句柄数,文件描述符是什么

linux文件句柄数 linux文件句柄数 1.问题阐述: too many open files:顾名思义即打开过多文件数. 不过这里的files不单是文件的意思,也包括打开的通讯链接(比如socket),正在监听的端口等等,所以有时候也可以叫做句柄(handle),这个错误通常也可以叫做句柄数超出系统限制. 2.产生的原因: 经常在使用linux的时候出现,大多数情况是由于程序没有正常关闭一些资源引起的,所以出现这种情况,请检查io读写,socket通讯等是否正常关闭. 3.经典案例: 很多

修改用户进程可打开文件数限制(转)

1.修改用户进程可打开文件数限制 在Linux平台上,无论编写客户端程序还是服务端程序,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量 的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄).可使用ulimit命令查看系统允许 当前用户进程打开的文件数限制: [[email protected] ~]$ ulimit -n 1024 这表示当前用户的每个进程最多允许同时打开1024个文件,这1024个