Linux : Count Open Files for Process

limitslinuxlsofprocessunix

How can I check how many open files are currently used?

Checking on PM2 is just an example:

I found the PID:

ps aux | grep pm2 | awk '{ print $2 }'

Checked that there is a limit of 65536 openfiles:

# cat /proc/16305/limits | grep 'Max open files'

Max open files 65536 65536 files

This limit is set via systemd:

# cat /etc/systemd/system/pm2.service.d/nofile_limit.conf
[Service]
LimitNOFILE=65536

I know that I can check how many files are open based on the user:

lsof -u UserName | wc -l
252

PM2 is running on diffrent user than PM2, and when I checked the limit on that user it is ridiculously small, so something is wrong here..

How can I check how many open files are currently used if the limit is set and managed by systemd?

When I wrote this post I came with idea, maybe this could be a solution to my own problem:

lsof | grep ' userName ' | awk '{print $NF}' | sort | wc -l
2348

What do you think?

Best Answer

If you want check for one process which you know pid try this

ls /proc/$pid/fd/ | wc -l