Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to monitor which processes access a particular file in Unix?

Tags:

c++

c

shell

unix

perl

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++)?

like image 652
Lazer Avatar asked Sep 08 '10 14:09

Lazer


4 Answers

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

like image 68
Carlos Valiente Avatar answered Nov 08 '22 00:11

Carlos Valiente


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).

like image 40
lucas1000001 Avatar answered Nov 07 '22 22:11

lucas1000001


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

like image 27
Daniel Voina Avatar answered Nov 08 '22 00:11

Daniel Voina


You might want to look at lsof.

like image 39
Chas. Owens Avatar answered Nov 08 '22 00:11

Chas. Owens