views:

130

answers:

4

I have a file and a lot of process (and process threads) are accessing it.

I want to monitor the file to get a listing of what all processes tried to access the file. Being able to record the timestamps also would be excellent for logging purposes, though I can do without it.

  • Is there any Unix utility that does something similar?

  • In case no such utility exists, how should I program this using a script (shell, Perl) or a program (C, C++)?

+5  A: 

I think the basic functionality you're looking for is in the UNIX command fuser.

This will tell you what processes are using a file (or port if you like).

lucas1000001
Although admittedly it does not make a time-based report - but should be easy enough to build on this to create what you need
lucas1000001
+1  A: 

You might want to look at lsof.

Chas. Owens
@Chas: A program might have opened it and closed it when I run `lsof`. In this case, it would not be registered with `lsof`. I need a way to continuously monitor the file so that whenever it is accessed by some process, a trigger logs the access to a logfile.
Lazer
+9  A: 

Under Linux, inotify might be what you're looking for.

Carlos Valiente
I second this! inotify also allows you to add hooks that run at certain times: when a file is accessed, written to, appended to, etc; I used it some time ago to automatically git commit files modified in a directory when they changed or were removed
mfontani
+2  A: 

FAM - File Alteration Monitor http://oss.sgi.com/projects/fam/ or Gamin http://people.gnome.org/~veillard/gamin/

might help you (gamin seems maintained while I cannot say the same for FAM).

You can attach your app to FAM/Gamin and then you'll be notified if something happened with the watched files. For details I suggest: http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi?coll=0650&db=bks&fname=/SGI_Developer/books/IIDsktp_IG/sgi_html/ch08.html

Daniel Voina