I'm trying to find a reliable way of finding which process on my machine is changing a configuration file (/etc/hosts
to be specific).
I know I can use lsof /etc/hosts
to find out what processes currently have the file open, but this doesn't help because the process is obviously opening the file, writing to it, and then closing it again.
I also looked at lsof
's repeat option (-r), but it seems to only go as fast as once a second, which probably won't ever capture the write in progress.
I know of a couple tools for monitoring changes to the filesystem, but in this case I want to know which process is responsible, which means catching it in the act.
You can use auditing to find this. If not already available, install and enable auditing for your distro.
set an audit watch on /etc/hosts
Wait till the hosts file changes and then use ausearch to seer what is logged
You'll get masses of output e.g.
In this case I used the touch command to change the files timstamp it's pid was 7294 and it's ppid was 7259 (my shell).
After a lot of search, I found the solution, just use this command:
sudo fs_usage | grep [path_to_file]
You can also use inotify-tools:
probably better to use something like incron then
http://inotify.aiken.cz/?section=incron&page=about&lang=en
you can then get it to trigger a script to so some sort of diags