0

How can I register to event that let me know when specific file is read in linux?

Can implement in c or in bash /shell script

  • https://unix.stackexchange.com/questions/12247/linux-file-access-monitoring – Kamaraj Apr 15 '19 at 06:48
  • @roaima The purported dupe asks a different thing -- the title is misleading; please read the actual text of the question. This question was probably already asked a lot of times, but not there. –  Apr 15 '19 at 07:03
  • @mosvy the answers on the suggested dup are applicable here too. They say the same as your answer. – Chris Davies Apr 15 '19 at 07:17
  • No, they're not applicable. They don't show how to monitor whenever a read(2) is done, as in my example. And they're cluttered with a lot of confusing stuff: loggedfs, .bash_history, etc. As I said, there probably is a dupe for this, but not that. –  Apr 15 '19 at 07:21
  • How about https://unix.stackexchange.com/questions/207304/how-to-monitor-the-opening-and-closing-of-files for a less-confusing duplicate? – JigglyNaga Apr 16 '19 at 11:59

1 Answers1

2

Use inotify(7), inotify_add_watch(2) with IN_ACCESS, inotifywait(1), etc.

Example: In a window:

while read f; do echo "$f"; sleep 3; done </path/to/your/file
...

In another window:

inotifywait -me access /path/to/your/file
/path/to/your/file ACCESS
/path/to/your/file ACCESS
/path/to/your/file ACCESS
...