<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[Linux查看文件句柄占用多的进程 ，查看Linux某进程的句柄数，并通过ulimit -n 单个进程打开的最大文件句柄数量限制解决accept4() failed (24: Too many open files)]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Unix/LinuxC技术]]></category>
<pubDate>Thu, 08 Aug 2013 06:49:54 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	查看当前进程的最大可以打开的文件数：<br/>cat /proc/PID/limits&nbsp;&nbsp;(如果通过ulimit -n 设置或者修改/etc/security/limits.conf，看看进程是否生效)&nbsp;&nbsp;<br/><br/>ulimit&nbsp;&nbsp;-a&#124;grep &quot;open files&quot;<br/>open files&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(-n) 1024000<br/><br/>直接对文件的修改如下：<br/> cat&nbsp;&nbsp;/etc/security/limits.conf&#124;grep -Ei &#039;soft&#124;hard&#039;<br/>* soft nofile 1024000<br/>* hard nofile 1024000<br/>* soft nproc&nbsp;&nbsp;1024000<br/>* hard nproc&nbsp;&nbsp;1024000<br/><br/>cat /proc/1279/limits &#124;grep files<br/>Max open files&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;8192&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8192&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; files&nbsp;&nbsp;&nbsp;&nbsp; <br/> cat /proc/64658/limits &#124;grep files<br/>Max open files&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1024000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1024000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;files&nbsp;&nbsp;&nbsp;&nbsp; <br/> cat /proc/64658/limits &#124;grep files<br/> cat /proc/18576/limits &#124;grep files<br/>Max open files&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1024000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1024000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;files&nbsp;&nbsp;&nbsp;&nbsp; <br/><br/>怎么修改？<br/># CentOS7系统使用命令<br/>prlimit --nofile=65536:65536 --pid 39977<br/><br/># CentOS6系统使用命令<br/>echo - n &quot;Max open files=65535:65535&quot; &gt; /proc/39977/limits<br/><br/>当然，可以通过重启这个进程所对应的服务（如果可重启，也不会来修改这个session里的参数了）来重新读取系统里的默认设置。<br/><br/>ulimit -n<br/># 临时修改，重启失效<br/>ulimit -HSn 65536<br/><br/># 永久解决<br/>vim /etc/security/limits.conf<br/># 添加如下的行<br/>* soft nproc 65536<br/>* hard nproc 65536<br/>* soft nofile 65536<br/>* hard nofile 65536<br/><br/>实践如下：<br/><textarea name="code" class="php" rows="15" cols="100">
cat /proc/1279/limits &#124;grep files
Max open files&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;8192&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8192&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; files&nbsp;&nbsp;&nbsp;&nbsp; 
prlimit --nofile=65536:65536 --pid 1279
cat /proc/1279/limits &#124;grep files
Max open files&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;65536&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;65536&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;files&nbsp;&nbsp;&nbsp;&nbsp; 
</textarea><br/>原文链接：https://blog.csdn.net/dqchouyang/article/details/115230076<br/><br/>Linux查看文件句柄占用多的进程 ：<br/><textarea name="code" class="php" rows="15" cols="100">
root@119.10.6.54:~# lsof -n&#124;awk &#039;&#123;print $2&#125;&#039;&#124;sort&#124;uniq -c&#124;sort -nr&#124;more
&nbsp;&nbsp;&nbsp;&nbsp;939 547
&nbsp;&nbsp;&nbsp;&nbsp;247 2940
&nbsp;&nbsp;&nbsp;&nbsp;208 484
&nbsp;&nbsp;&nbsp;&nbsp;177 32740
&nbsp;&nbsp;&nbsp;&nbsp;160 30411
&nbsp;&nbsp;&nbsp;&nbsp;159 32614
&nbsp;&nbsp;&nbsp;&nbsp;157 8799
&nbsp;&nbsp;&nbsp;&nbsp;157 8798
&nbsp;&nbsp;&nbsp;&nbsp;157 8797
&nbsp;&nbsp;&nbsp;&nbsp;157 8770
&nbsp;&nbsp;&nbsp;&nbsp;157 8769
&nbsp;&nbsp;&nbsp;&nbsp;157 8764
&nbsp;&nbsp;&nbsp;&nbsp;157 8763
&nbsp;&nbsp;&nbsp;&nbsp;157 8713
&nbsp;&nbsp;&nbsp;&nbsp;157 8618
</textarea><br/>其中第一列是打开的句柄数，第二列是进程ID。<br/>可以根据ID号来查看进程名和进程的句柄详细情况:<br/><textarea name="code" class="php" rows="15" cols="100">
ls /proc/18576/fd&#124;wc -l
32

cat /proc/18576/limits &#124;grep files
Max open files&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1024000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1024000&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi


 ps -aux&#124;grep 547
root&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 547 62.9 24.7 3329596 1480976 ?&nbsp;&nbsp;&nbsp;&nbsp; Sl&nbsp;&nbsp; Jan20 37395:06 /usr/lib/virtualbox/VirtualBox --comment MyXpSer --startvm c91a5bb6-bcc1-4a94-8ae3-9cce9ce40f42 --no-startvm-errormsgbox

ps aux&#124;grep 2940
root&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2940&nbsp;&nbsp;0.0&nbsp;&nbsp;0.0&nbsp;&nbsp;64704&nbsp;&nbsp; 284 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ss&nbsp;&nbsp; Jan15&nbsp;&nbsp; 0:02 /usr/sbin/certmonger -S -p /var/run/certmonger.pid

ps -aux&#124;grep 484
root&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 484&nbsp;&nbsp;1.8&nbsp;&nbsp;0.0 1067524 4340 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sl&nbsp;&nbsp; Jan20 1079:59 /usr/lib/virtualbox/VirtualBox

 ps -aux&#124;grep 32740
root&nbsp;&nbsp;&nbsp;&nbsp; 32740&nbsp;&nbsp;0.0&nbsp;&nbsp;0.0 442696&nbsp;&nbsp;3936 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;S&nbsp;&nbsp;&nbsp;&nbsp;Jan20&nbsp;&nbsp; 7:17 /usr/libexec/clock-applet --oaf-activate-iid=OAFIID:GNOME_ClockApplet_Factory --oaf-ior-fd=28

ps -aux&#124;grep 30411
root&nbsp;&nbsp;&nbsp;&nbsp; 30411&nbsp;&nbsp;0.0&nbsp;&nbsp;0.0 258724&nbsp;&nbsp; 940 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ss&nbsp;&nbsp; Feb17&nbsp;&nbsp;16:36 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)&nbsp;&nbsp;

root@119.10.6.23:~# ps -aux&#124;grep 32614
root&nbsp;&nbsp;&nbsp;&nbsp; 32614&nbsp;&nbsp;0.0&nbsp;&nbsp;0.0 500548&nbsp;&nbsp;3348 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sl&nbsp;&nbsp; Jan20&nbsp;&nbsp;13:58 gnome-panel

 ps -aux&#124;grep 8799
www&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8799&nbsp;&nbsp;2.6&nbsp;&nbsp;0.1 259128&nbsp;&nbsp;6556 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;S&nbsp;&nbsp;&nbsp;&nbsp;16:34&nbsp;&nbsp; 0:15 php-fpm: pool www 
</textarea><br/>8798 8770 8797 8770 8769 8764 8763 8713 8618 均是PHP的PHP-FPM的句柄数，也就是说第一是virtualBOX，再才是certmonger服务，后是clock-applet，再后是php-fpm进程，这些进程文件数分布排列很正常~<br/>(1)这个没啥用就关了得了：<br/>certmonger 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭<br/>chkconfig certmonger&nbsp;&nbsp;off<br/>root@119.10.6.54:~# chkconfig certmonger&nbsp;&nbsp;off<br/>root@119.10.6.54:~# chkconfig --list&#124;grep certmonger<br/>certmonger&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0:关闭&nbsp;&nbsp;1:关闭&nbsp;&nbsp;2:关闭&nbsp;&nbsp;3:关闭&nbsp;&nbsp;4:关闭&nbsp;&nbsp;5:关闭&nbsp;&nbsp;6:关闭<br/>root@119.10.6.54:~# service certmonger stop<br/>停止 certmonger：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[确定]<br/><br/>重启后按Esc或者F5键，可以查看系统启动过程，卡在哪里，我的是在certmonger启动OK之后卡住的，完全有必要停掉，来源：<br/>http://www.linuxidc.com/Linux/2015-02/112688.htm<br/><br/>(2)/usr/libexec/clock-applet --oaf-activate-iid=OAFIID:GNOME_ClockApplet_Factory --oaf-ior-fd=29<br/>这个好像是gnome的时间同步啥的一个服务，暂时不关。<br/>（1）查看Linux系统默认的最大文件句柄数，系统默认是1024<br/><br/>(3)对服务器没有用的也关了，调节cpu速度用来省电，常用在Laptop上：<br/>service cpuspeed stop<br/>chkconfig cpuspeed off<br/><br/>——————————————————————————————————————————————————<br/>ulimit -n<br/>1024<br/><br/>（2）查看当前进程打开了多少句柄数<br/>lsof -n&#124;awk &#039;&#123;print $2&#125;&#039;&#124;sort&#124;uniq -c&#124;sort -nr&#124;more<br/>131 24204　<br/>57 24244　　<br/>57 24231　　 ........<br/>其中第一列是打开的句柄数，第二列是进程ID。<br/><br/>（3）可以根据ID号来查看进程名。<br/>ps aef&#124;grep 24204<br/>nginx　　24204 24162 99 16:15 ?　　　　00:24:25 /usr/local/nginx/sbin/nginx -s<br/><br/>来自：http://www.examw.com/linux/all/146041/<br/><br/>accept4() failed (24: Too many open files)<br/>如何优化这个<br/>ulimit -n 是多少？<br/>文件描述符不够用了 <br/>1024<br/>这个可以开大点 <br/>能开多大，如何去衡量啊<br/>内存足够都可以 <br/>一般开10万 <br/>好的，我看看。<br/>来源：PHP高级编程QQ对话。<br/>=========================================<br/>实践如何找到当面数据库打开了多少个文件句柄数的如下实践：<br/>1）先PS一下获取到Mysql的进程号：<br/>root&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4935&nbsp;&nbsp;0.0&nbsp;&nbsp;0.0&nbsp;&nbsp;63992&nbsp;&nbsp; 272 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;S&nbsp;&nbsp;&nbsp;&nbsp;Aug06&nbsp;&nbsp; 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/conf/my.cnf<br/>mysql&nbsp;&nbsp;&nbsp;&nbsp; 5622&nbsp;&nbsp;4.5 83.4 14974096 6823984 ?&nbsp;&nbsp;&nbsp;&nbsp;Sl&nbsp;&nbsp; Aug06 149:07 /usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/conf/my.cnf --basedir=/usr/local/mysql --datadir=/data/db/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/db/mysql/localhost.localdomain.err --open-files-limit=65535 --pid-file=/data/db/mysql/localhost.localdomain.pid --socket=/tmp/mysqld.sock --port=3306<br/>2）得到如下两个进程号：<br/>4935<br/>5622<br/><br/>３)通过lsof获取到所有进程的句柄数再grep一下就得到了当前mysql的进程为4935的句柄数：<br/>/usr/sbin/lsof -n&#124;awk &#039;&#123;print $2&#125;&#039;&#124;sort&#124;uniq -c &#124;sort -nr&#124;grep 4935 <br/>4 4935<br/>4）一样的方法：<br/>/usr/sbin/lsof -n&#124;awk &#039;&#123;print $2&#125;&#039;&#124;sort&#124;uniq -c &#124;sort -nr&#124;grep 5622 <br/>4 5622<br/><br/>目前只打开了4个文件句柄数量，远远底于系统默认值1024。<br/><br/>=========================================<br/>在系统访问高峰时间以root用户执行上面的脚本，可能出现的结果如下：<br/>&nbsp;&nbsp;&nbsp;&nbsp;# lsof -n&#124;awk &#039;&#123;print $2&#125;&#039;&#124;sort&#124;uniq -c &#124;sort -nr&#124;more&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;131 24204&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 57 24244&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 57 24231&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 56 24264&nbsp;&nbsp;<br/>其中第一行是打开的文件句柄数量，第二行是进程号。得到进程号后，我们可以通过ps命令得到进程的详细内容。<br/>&nbsp;&nbsp;&nbsp;&nbsp;ps -aef&#124;grep 24204&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;mysql&nbsp;&nbsp;&nbsp;&nbsp;24204 24162 99 16:15 ?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;00:24:25 /usr/sbin/mysqld&nbsp;&nbsp;<br/>哦，原来是mysql进程打开最多文件句柄数量。但是他目前只打开了131个文件句柄数量，远远底于系统默认值1024。<br/>但是如果系统并发特别大，尤其是squid服务器，很有可能会超过1024。这时候就必须要调整系统参数，以适应应用变化。Linux有硬性限制和软性限制。可以通过ulimit来设定这两个参数。方法如下，以root用户运行以下命令：<br/>&nbsp;&nbsp;&nbsp;&nbsp;ulimit -HSn 4096&nbsp;&nbsp;<br/>以上命令中，H指定了硬性大小，S指定了软性大小，n表示设定单个进程最大的打开文件句柄数量。个人觉得最好不要超过4096，毕竟打开的文件句柄数越多响应时间肯定会越慢。设定句柄数量后，系统重启后，又会恢复默认值。如果想永久保存下来，可以修改.bash_profile文件，可以修改 /etc/profile 把上面命令加到最后。<br/>用普通用户登录，执行ulimit -n，查看文件打开数。<br/>如果太小，默认值是1024，则进行如下操作:<br/>使用root用户修改vim /etc/security/limits.conf 文件。<br/>在文件最后加如下两句话:<br/>*&nbsp;&nbsp; soft nofile 50000<br/>*&nbsp;&nbsp; hard nofile 50000<br/>参考：http://hi.baidu.com/itnote/item/7347bfea829e3ee3fa42bad8
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] Linux查看文件句柄占用多的进程 ，查看Linux某进程的句柄数，并通过ulimit -n 单个进程打开的最大文件句柄数量限制解决accept4() failed (24: Too many open files)]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>