To view the number of FD in a process and how to modify the system configuration

Source: Internet
Author: User

Under Linux, we use the ulimit-n command to see the maximum number of file handles that a single process can open (the socket is also included). system default value 1024.

   for general applications (like Apache, System process) 1024 is completely enough to use. But how to deal with a large number of requests like squid, MySQL, Java and other single processes is a bit stretched. If the number of file handles opened by a single process exceeds the system-defined value, the error message "Too many files open" is mentioned. How do I know how many file handles the current process has opened? Here are some little feet that can help you see:

lsof-n |awk ' {print $} ' |sort|uniq-c |sort-nr|more   
during peak system access time, the above script is executed as root, and the results may appear as follows:

# Lsof-n|awk ' {print $} ' |sort|uniq-c |sort-nr|more   
    131 24204 
    24244 
    24231 
    24264 
The first line is the number of open file handles, and the second line is the process number. After we get the process number, we can get the detailed contents of the process through the PS command.

Ps-aef|grep 24204 
MySQL   24204 24162 16:15?       00:24:25/usr/sbin/mysqld 
Oh, it turns out that the MySQL process opened the maximum number of file handles. But he's currently only open 131 file handles, far below the system default of 1024.

But if the system is particularly large, especially squid servers, it is likely to exceed 1024. It is necessary to adjust the system parameters to adapt to the application changes. Linux has hard limits and soft limits. These two parameters can be set by Ulimit. To do this, run the following command as the root user:

ULIMIT-HSN 4096 
in the above command, h specifies the hard size, s specifies the soft size, and n indicates the maximum number of open file handles that are set for a single process. Personally think it's best not to exceed 4096, after all, the more open file handle number of response time will certainly be slower. When the number of handles is set, the default value is restored after the system restarts. If you want to save it permanently, you can modify/etc/profile to add the above command to the last.


To view the number of FD in a process and how to modify the system configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.