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 command documentation for more information

FreeBSD list open files per process

On FreeBSD use the fstat command along with the ps command:
# ps aux | grep -i openvpn # filter outputs using the grep command #
# fstat -p {PID}
# fstat -p 1219

We can count open files count for openvpn process as follows using the wc command:
# fstat -p 1219 | grep -v ^USER | wc -l
The -p option passed to the fstat to report all files open by the specified process.

FreeBSD show open files per process command

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 use bash for loop as 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

Ubuntu Linux List Open Files for Process Bash Command

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. See how to increase the system-wide/user-wide number of available (open) file handles on Linux for more information.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值