home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lsof_3.37 / scripts / watch_a_file.perl < prev   
Encoding:
Text File  |  1995-07-31  |  1.4 KB  |  62 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # watch_a_file.perl -- use lsof -F output to watch a specific file
  4. #               (or file system)
  5. #
  6. # usage:    watch_a_file.perl file_name
  7.  
  8. ## Interrupt handler
  9.  
  10. sub interrupt { wait; print "\n"; exit 0; }
  11.  
  12.  
  13. ## Start main program
  14.  
  15. $Pn = "watch_a_file";
  16. # Check file argument.
  17.  
  18. if ($#ARGV != 0) { print "$#ARGV\n"; die "$Pn usage: file_name\n"; }
  19. $fnm = $ARGV[0];
  20. if (! -r $fnm) { die "$Pn: can't read $fnm\n"; }
  21.  
  22. # Do setup.
  23.  
  24. $LSOF = "../lsof";            # path to lsof
  25. $RPT = 15;                # lsof repeat time
  26. $| = 1;                    # unbuffer output
  27. $SIG{'INT'} = 'interrupt';        # catch interrupt
  28. if ( ! -x $LSOF) { print "can't execute $LSOF\n"; exit 1 }
  29.  
  30. # Read lsof -HPF output from a pipe and gather the PIDs of the processes
  31. # and file descriptors to watch.
  32.  
  33. open(P, "$LSOF -HPFpf $fnm|") || die "$Pn: can't pipe to $LSOF\n";
  34.  
  35. $curpid = -1;
  36. $pids = "";
  37. while (<P>) {
  38.     chop;
  39.     if (/^p(.*)/) { $curpid = $1; next; }    # Identify process.
  40.     if (/^f/) {
  41.     if ($curpid > 0) {
  42.         if ($pids eq "") { $pids = $curpid; }
  43.         else { $pids = $pids . "," . $curpid; }
  44.         $curpid = -1;
  45.     }
  46.     }
  47. }
  48. close(P);
  49. wait;
  50. if ($pids eq "") { die "$Pn: no processes using $fnm located.\n"; }
  51. print "watch_file: $fnm being used by processes:\n\t$pids\n\n";
  52.  
  53. # Read repeated lsof output from a pipe and display.
  54.  
  55. $pipe = "$LSOF -ap $pids -r $RPT $fnm";
  56. open(P, "$pipe|") || die "$Pn: can't pipe: $pipe\n";
  57.  
  58. while (<P>) { print $_; }
  59. close(P);
  60. print "$Pn: unexpected EOF from \"$pipe\"\n";
  61. exit 1;
  62.