12

The files opened by XYZ process can be found with the command

ls -l /proc/PID/fd

Is there anyway that can be done in a more interactive way like tail auto-refreshing every x seconds?

17

Try the watch command:

watch -n 10 ls -l /proc/$$/fd

Watch is nice.

You could use an old school while loop:

while :
do
 ls -l /proc/$$/fd
 sleep 10
done

watch is in the procps package on debian based systems and the procps rpm on RedHat derived systems.

1
  • 1
    sleep itself can be used as condition in while loop, so your example can be written more elegantly like this: while sleep 10; do ls -l /proc/$$/fd; done
    – ipozgaj
    Jan 6 '11 at 12:55
12

If you want to see each file as it is being opened, you can filter that with strace. For example:

strace -p _pid_of_app_ -e trace=open,close
1
  • 5
    This really is the best answer. I'd recommend the flags -y and -f if you're attempting to debug something running in a shell. Just pass the shell PID in and -f will follow any forks.
    – Aea
    Sep 25 '17 at 22:42
4

You could combine lsofand watch.

For example watch "lsof -p 1234" will give you a list of all open files of pid 1234 every 2 seconds. You could change some parameters to meet your needs.

2
  • it will not give a list EVERY 2 seconds. The parameter -r 2 is missing for that to work! -p is the PID. The answer is very poor! Aug 16 '17 at 6:51
  • 2
    Nice, wasn't aware of the -r option for lsof. Therefore I used watch to execute lsof every two seconds.
    – Christian
    Aug 16 '17 at 8:57
1

I created a bash file where I was writing the output of the command to a file. File was generated on the basis current date. Here I am counting number of open files.

#!/bin/bash
while :
do
 cd /proc/<PID>/fd
 today=$(date +"%m-%d-%Y")
 filename="/tmp/${today}.txt"
 ls -l | wc -l >> "${filename}"
 sleep 10
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.