Finding the max file count in Linux

Finding the max file count in Linux

Introduction

There are multiple instances where you’ll need to know the number of files that are kept open by a process, or see those files etc. If you are working on production environments, using commands like lsof or pfile etc will not be possible because you cannot install them on production servers without permission.

So the following commands can be used instead. They use the basic linux commands to achieve the same effect.

Scenario

Lets just assume your web application is deployed using tomcat and it is misbehaving and the log says “Too many files open”.

If you are facing this issue, the root cause could be that the web application is opening some files for processing and it might be deleting them also after processing but it might not be closing it before deleting it. Even though the file is deleted, it will remain open if it is not explicitly closed by the application.

From experience

Now you need to know how many files are open, what the limits are and which all files are open.

Find the open file count

To find the open file count of that tomcat process, follow the instructions

Find the Tomcat process ID

Command

sudo ps aux | grep tomcat

Output shows the user, process ID, etc.

Note down the process ID – Let’s say it is 20220

Check the open file count

Now you can use that process ID in the following command to see the number of files kept open by that tomcat process

sudo ls /proc/20220/fd | wc -l

This shows the number of files which are kept open.

Check the limit

To check the limit for the process (will be the limit of the user that is running the process), run the following command.

sudo cat /proc/20220/limits

You’ll see something like the following line which shows the open file limit.

Max open files        1024          1048576         files

Check the files that are kept open

Now you’ll need to see which are the files that your application is keeping open.

For that use the following command

sudo ls -lh /proc/20220/fd