To modify the number of file handles under Linux, we use the ulimit-n command to see the maximum number of file handles that a single process can open (the socket connection is also included). The system defaults to 1024.
For general applications (like Apache, System process) 1024 is fully enough to use. But how to handle a lot of requests like squid, MySQL, Java and other applications is a bit stretched. If the number of file handles opened by a single process exceeds the system-defined value, the error prompts "Too many files open" are mentioned. How do I know how many file handles the current process has open? Here is a little bit of a little foot that can help you see:
1.lsof-n |awk ' {print $} ' |sort|uniq-c |sort-nr|more
The above script is executed at the peak time of system access with the root user, and the following results may occur:
1.# Lsof-n|awk ' {print $} ' |sort|uniq-c |sort-nr|more
2.131 24204
3.57 24244
4.57 24231
5.56 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 details of the process through the PS command.
1.ps-aef|grep 24204
2.mysql 24204 24162 99 16:15? 00:24:25/usr/sbin/mysqld
Oh, that was the MySQL process. The maximum number of file handles is open. But he currently has only 131 file handles open, far below the system default value of 1024.
But if the system is exceptionally large, especially squid servers, it is likely to exceed 1024. At this point, you must adjust the system parameters to adapt to the application changes. Linux has hard limits and soft limits. You can set these two parameters by Ulimit. As follows, run the following command with the root user:
1.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 set a single process. Personally feel better not to exceed 4096, after all, the more open file handle number of response time will certainly be more slowly. After the number of handles is set, the default value is restored after the system restarts. If you want to keep it permanently, you can modify/etc/profile to add the above command to the end. (Findsun proposed method is more reasonable)
/////////////////////////////////////////////////////////////////////////////
When deploying applications under Linux, there are times when you encounter the problem of Socket/file:can ' t open so many files, but Linux is limited by file handles (like WinXP?). , and the default is not very high, is generally 1024, as a production server, in fact, it is easy to reach this number, so we need to change the value of some.
Probably know ulimit This command is relevant, Google search for a moment, most said very vague, there is no unified say, after two hours to see a lot of articles finally understand Chu Ulimit related to some configuration problems.
We can use ULIMIT-A to see all the limits, I only care about the number of file handles
Open files (-N) 1024
This is the limit quantity.
Here, there are many ulimit articles are very vague, whether this 1024 is a system of restrictions, or user restrictions. In fact, this is a user-restricted, complete statement, should be the current user ready to run the program restrictions.
1. This limit is for a single program
2. This restriction does not change the limitations of the program that was previously run
3, the change to this value, exit the current shell will disappear
For example, I ran a program a first, then changed the limit to 2048 by Ulimit, then ran B, then exited the shell and logged in, then ran C. Then only B can open 2048 handles.
What should we do if we need to change the overall limit, or if the program we're running is system-initiated?
One way to do this is to ulimit the change command into the/etc/profile, but that's not a good idea.
The correct approach should be to modify the/etc/security/limits.conf
There are very detailed notes in it, such as
* Soft Nofile 2048
* Hard Nofile 32768
You can change the file handle limit to soft 2048, hard 32768
Here is another question, what is a soft limit and what is a hard limit
The hard limit is the actual limit, while the soft limit is warnning limited and will only make warning
In fact, Ulimit command itself on the soft and hard settings, plus-H is hard, plus-s is soft
The default display is the soft limit, if the change is not added, is two to change together
The first one in the configuration file is domain, set to the asterisk to represent the global, and you can also make different restrictions for different users
Modified, login with Ulimit immediately after the opening, but the previously launched program to restart to use the new value. I'm using CentOS, and it seems that some systems need to be restarted to take effect.
Ulimit is actually a restriction on a single program.
What about the total system limit?
It's actually here,/proc/sys/fs/file-max.
You can see the current value through cat, Echo to modify immediately
There's another one,/proc/sys/fs/file-nr.
Read-only to see the number of file handles currently used by the entire system
When looking for file handle problems, there is also a very useful program lsof
It's convenient to see a process open those handles.
You can also see what process a file/directory is taking up.
Above this Linux view process open file handle number and how to modify the method is small to share all the content of everyone, hope to give you a reference, but also hope that we support cloud habitat community.