Linux – Find processes which have a file open without lsof or fuser

filesfuserlinuxlsof

I'm working on a Linux (Scientific Linux CERN SLC release 6.9 (Carbon))
machine on which I am unable to install programs and on which the lsof or fuser commands are not available.

I'm trying to remove an NFS dotfile on this machine but I keep getting the Device or resource busy error so I'd like to find out which process (I suspect it might be one I have previously started with nohup) still has a file descriptor to this file.

How can I achieve this?

Best Answer

Use /proc/<PID>/fd.

Example....we want to figure out which pid has /var/log/audit/audit.log open. fuser tells us it's pid 255.

[root@instance-1 ~]# fuser /var/log/audit/audit.log
/var/log/audit/audit.log:   255
[root@instance-1 ~]#

So using a non fuser solution:

[root@instance-1 ~]# find /proc/*/fd -ls|grep /var/log/audit/audit.log
188652    0 l-wx------   1 root     root           64 Jul  1 06:22 /proc/255/fd/5 -> /var/log/audit/audit.log
[root@instance-1 ~]#
Related Question