Continuously monitor files opened/accessed by a process
Solution 1
Try with strace -p 12345
; it should do what you are trying to achieve.
The output can be filtered to only display opened files (Dan D.'s comment):
strace -e open -p 12345
Note: you can also trace quickly running processes with strace -e open <command>
.
Solution 2
The new utility fatrace will do this: https://launchpad.net/fatrace/
sudo fatrace | grep '(6514)'
Don't use the -p option, it means the opposite of what it means in lsof or other utilities.
Solution 3
This will loop re-running your command and clearing the screen each time:
watch "lsof -p 12345"
WARNING: this will miss quick file accesses and is only suitable to see long standing files
Related videos on Youtube
MA1
Updated on September 18, 2022Comments
-
MA1 over 1 year
lsof -p 12345
will list all the files opened by process whose pid is 12345 but only for a particular instant of time.How can we continuously monitor a process from the start to end(until process is terminated) to list/show every single file accessed by the process during its whole lifetime?
-
phuclv about 7 years
-
-
MA1 over 12 yearsoutput is not friendly and too much extra things.
-
Dan D. about 12 years@Ninefingers Actually
strace
can do that better thangrep
with the-e
option:strace -e open
-
andyB about 12 years@DanD oh yeah, ofc :)
-
David Foerster over 10 yearsThis is somewhat clumsy compared to the other answer using
strace
. -
Dor over 10 yearsThat's inaccurate solution - a process may use files in between executions of
lsof
-
Jordon Bedwell over 10 years@Dor you can set the timing of lsof to sub 1 second and increase it's precision. While it's clumsy compared to others, you are wrong in that it's an inaccurate solution.
-
jcalfee314 over 10 yearsIf your looking at a long file operation (like a database backup) this may a good simple alternative.
-
CMCDragonkai about 9 yearsWhen I kill the strace command, it also kills the thing it is tracing. Why is this happening (cygwin)?
-
Jens Erat about 9 yearsSounds like a bug. Be aware that the cygwin-
strace
is probably not the Linux-strace
, asstrace
is a Linux-specific tool. Cygwin builds a Unix-compatiblity layer, and does not try to be Linux. With cygwin, you're probably better off using the original Windows tools.