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?
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.
To find out the list of files opened by parent process Id lsof command is used with the option -R.
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With