Linux / UNIX List Open Files for Process
Linux / UNIX List Open Files for Process
How do I list all open files for a Linux or UNIX process using command line options? How can I show open files per process under Linux?
Both Linux and Unix-like operating systems come with various utilities to find out open files associated with the process.
UNIX List Open Files For Process
First use the ps command command to get PID of process, enter:$ ps -aef | grep {process-name}
$ ps -aef | grep httpd
Next pass this PID to pfiles command,$ pfiles {PID}
$ pfiles 3533
See pfiles commanddocumentationfor more information
FreeBSD list open files per process
On FreeBSD use the fstat command along with the ps command:
# ps aux | grep -i openv*n# filter outputs using thegrep command#
# fstat -p {PID}
# fstat -p 1219
We can count open files count for openv*n process as follows using the wc command:
# fstat -p 1219 | grep -v ^USER | wc -l
The-poptionpassed to the fstat to report all files open by the specified process.
FreeBSD pstat command in action
Linux List Open Files For Process
First you need to find out PID of process. Simply use any one of the following command to obtain process id:# ps aux | grep {program-name}
OR$ ps -C {program-name} -o pid=
For example, find out PID of firefox web-browser, enter:$ ps -C firefox -o pid=
Output:
7857
To list opne files for firefox process, enter:$ ls -l /proc/7857/fd
Sample output:
total 0 lr-x------ 1 vivek vivek 64 2008-03-05 22:31 0 -> /dev/null l-wx------ 1 vivek vivek 64 2008-03-05 22:31 1 -> pipe:[18371] lr-x------ 1 vivek vivek 64 2008-03-05 22:31 10 -> /usr/lib/firefox/firefox l-wx------ 1 vivek vivek 64 2008-03-05 22:31 2 -> pipe:[18371]
For privileged process use the sudo command and to count open files use the wc command on Linux as follows:# Get process pid
sudo ps -C Xorg -o pid
sudo ls -l /proc/${pid-here}/fd
# Say pid is 9497 for Xorg, then
sudo ls -l /proc/9497/fd | wc -l
We can usebash for loopas follows too:
# Linux Count and List Open Files for Nginx Process # SEARCH="nginx" for i in $(ps -C "${SEARCH}" -o pid | grep -v PID) do echo "PID # ${i} open files count : $(sudo ls -l /proc/${i}/fd | wc -l)" done |
Listing Open Files on Linux
Using lsof to display the processes using the most file handles
The lsof command list open files under all Linux distributions or UNIX-like operating system. Type the following command to list open file for process ID 351:$ lsof -p 351
In this example display and count all open files for top 10 processes on Linux operating systems or server:# lsof | awk '{print $1}' | sort | uniq -c | sort -r | head
## force numeric sort by passing the '-n' option to the sort ##
# lsof | awk '{print $1}' | sort | uniq -c | sort -r -n | head
3884 nginx 643 php-fpm7. 370 memcached 90 rsyslogd 81 systemd 63 systemd-j 58 systemd-r 55 systemd-n 50 systemd-l 48 networkd-
Where,
- lsof– Run the lsof to display all open files and send output to the awk
- awk '{print $1}'– Display first field i.e. process name only
- uniq -c– Omit duplicate lines while prefix lines by the number of occurrences
- sort -r– Reverse sort
- head– Display top 10 process along with open files count
Conclusion
Now you know how to find open files per process on Linux, FreeBSD, and Unix-like systems using various command-line options. Seehow to increase the system-wide/user-wide number of available (open) file handles on Linuxfor more information.