對於一般的應用來說(像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查看系統當前參數設置
[root@localdomain ~]# 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