Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux track all files accessed by process?

Tags:

file

linux

trace

Is there a way to track all file I/O for a given process? All I really need is the locations of files being read from/written to from a given process (and ideally if it was a read or write operation although that's not as important).

I can run the process and track it rather than needing to attach to an existing process which I would assume is significantly simpler. Is there any kind of wrapper utility I can run a process though that will monitor file access?

like image 683
Tom B Avatar asked Dec 11 '14 16:12

Tom B


People also ask

How Linux keeps track of files opened in a process?

The kernel keeps the data under /proc. Information about a process is in the directory /proc/<pid_the_process>. It contains entries for everything opened by the process file, named by its file descriptor, which is linked to the actual file.

How do you list out all files that are open by processes running on the Linux system?

To find out the list of files opened by parent process Id lsof command is used with the option -R.


2 Answers

lsof:

Try doing this as a starter :

lsof -p <PID>

this command will list all currently open files, fd, sockets for the process with the passed process ID.

For your special needs, see what I can offer as a solution to monitor a php script :

php foo.php & _pid=$!
lsof -r1 -p $_pid
kill %1 # if you want to kill php script

strace:

I recommend the use of strace. Unlike lsof, it stays running for as long as the process is running. It will print out which syscalls are being called when they are called. -e trace=file filters only for syscalls that access the filesystem:

sudo strace -f -t -e trace=file php foo.php

or for an already running process :

sudo strace -f -t -e trace=file -p <PID>
like image 158
Gilles Quenot Avatar answered Oct 11 '22 12:10

Gilles Quenot


Besides strace there is another option which does not substantially slow down the monitored process. Using the Liunx kernel's fanotify (not to be confused with the more popular inotify) it is possible to monitor whole mount-points for IO-activity. With unshared mountnamespaces the mounts of a given process can be isolated fromt the rest of the system (a key technology behind docker).

An implementation of this concept can be found in shournal, which I am the author of.

Example on the shell:

$ shournal -e sh -c 'cat foo > bar'
$ shournal --query --history 1
...
  1 written file(s):
     /home/user/bar
  1 read file(s):
     /home/user/foo 
like image 44
spawn Avatar answered Oct 11 '22 12:10

spawn