View the number of file handles opened by the process under Linux and how to modify the method

  • 2020-05-13 04:21:50
  • OfStack

Modify the number of file handles under Linux, we use the ulimit-n command to see the maximum number of file handles a single process can open (socket connections are also included). The system default is 1024.

For 1-like applications (like Apache, system processes)1024 is perfectly adequate. But applications like squid, mysql, and java, where a single process handles a large number of requests, are a bit stretched. The "too many files open" error is mentioned if the number of file handles opened by a single process exceeds the system-defined value. How do you know how many file handles are open by the current process? Here's a little script to help you check it out:

1.lsof -n |awk '{print $2}'|sort|uniq -c |sort -nr|more

Executing the above script as an root user during peak system access times can result in the following:

1.# lsof -n|awk '{print $2}'|sort|uniq -c |sort -nr|more
2. 131 24204
3. 57 24244
4. 57 24231
5. 56 24264

Where line 1 is the number of open file handles and line 2 is the process number. Once we have the process number, we can get the process details from the ps command.

1.ps -aef|grep 24204
2.mysql 24204 24162 99 16:15 ? 00:24:25 /usr/sbin/mysqld

Oh, so it is the maximum number of file handles open by the mysql process. But he has only opened 131 file handles so far, well below the system default of 1024.

But if the system is extremely concurrent, especially the squid server, it is likely to exceed 1024. At this point, the system parameters must be adjusted to adapt to the application changes. Linux has hard and soft limits. These two parameters can be set by ulimit. Run the following command as an root user:

1.ulimit -HSn 4096

In the above command, H specifies the hard size, S specifies the soft size, and n specifies the maximum number of open file handles for a single process. Personally, it is better not to exceed 4096. After all, the more open file handles, the slower the time will be. After setting the number of handles, the system restarts and the default value is restored. If you want to save it forever, you can modify /etc/profile to add the above command to the end. (the method proposed by findsun is more reasonable)

/////////////////////////////////////////////////////////////////////////////

When deploying an application under Linux, you will sometimes encounter the problem of Socket/File: Can't open so many files, which is limited to file handles (like WinXP?). , and the default is not very high, 1 is generally 1024, as a production server, it is very easy to achieve this number, so we need to change this value by 1.

I probably know that the command ulimit is relevant. I searched for 1 on Google, but most of them were very vague and did not say 1. After reading many articles for two hours, I finally figured out some configuration problems related to ulimit.

We can use ulimit-a to see all the limits, I'm only concerned with the number of file handles

open files (-n) 1024

This is the limit quantity

Here, a lot of ulimit articles are vague about whether the 1024 is a system limit or a user limit. In fact, this is the user limit, in full, the limit of the application that the current user is going to run.

1. This limitation is for a single program

2. This limit does not change the limits of programs that have been run before

3. If you modify this value, the current shell will disappear

For example, I ran a program A, then modified the limit to 2048 via ulimit, then ran B, then logged out of shell, then logged in, and then ran C. Then only B can open 2048 handles.

What if we need to change the overall limit, or if we're running a program that the system started

One of the methods is to put the ulimit modify command into /etc/profile, but that's not a good idea

The right thing to do, it should be modified/etc security/limits conf

It has very detailed notes, for example

* soft nofile 2048

* hard nofile 32768

You can change the file handle constraint 1 to soft 2048, hard 32768

There is another question involved here, what is the soft limit and what is the hard limit

The hard limit is the actual limit, and the soft limit, which is the warnning limit, only makes warning

In fact, the ulimit command itself has soft and hard Settings, plus -H is hard, plus -S is soft

The default display is a soft limit, if not added when the modification, is two 1 from change

The first bit of the configuration file is domain, which is set to an asterisk to represent the whole thing, and you can restrict it to different users

Yes, relogging in with ulimit1 will take effect immediately, but previously started programs will have to be restarted to use the new values. I used CentOS and it seems that some systems need to be rebooted to take effect.

ulimit is really a restriction on a single 1 program

What about the total system limit

Is here, / proc sys/fs/file - max

You can view the current value with cat, which you can modify immediately with echo

Plus 1, / proc sys/fs/file - nr

Read-only, you can see how many file handles the entire system is currently using

There is also a very useful program lsof for finding file handle problems

It's easy to see if a process has opened those handles

You can also see what process is holding a file/directory.


Related articles: