Server IP : 85.214.239.14 / Your IP : 18.216.82.132 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/3/root/usr/share/doc/lsof/examples/ |
Upload File : |
#!/usr/bin/perl # # watch_a_file.pl -- use lsof -F output to watch a specific file # (or file system) # # usage: watch_a_file.pl file_name ## Interrupt handler sub interrupt { wait; print "\n"; exit 0; } ## Start main program $Pn = "watch_a_file"; # Check file argument. if ($#ARGV != 0) { print "$#ARGV\n"; die "$Pn usage: file_name\n"; } $fnm = $ARGV[0]; if (! -r $fnm) { die "$Pn: can't read $fnm\n"; } # Do setup. $RPT = 15; # lsof repeat time $| = 1; # unbuffer output $SIG{'INT'} = 'interrupt'; # catch interrupt # Set path to lsof. if (($LSOF = &isexec("../lsof")) eq "") { # Try .. first if (($LSOF = &isexec("lsof")) eq "") { # Then try . and $PATH print "can't execute $LSOF\n"; exit 1 } } # Read lsof -nPF output from a pipe and gather the PIDs of the processes # and file descriptors to watch. open(P, "$LSOF -nPFpf $fnm|") || die "$Pn: can't pipe to $LSOF\n"; $curpid = -1; $pids = ""; while (<P>) { chop; if (/^p(.*)/) { $curpid = $1; next; } # Identify process. if (/^f/) { if ($curpid > 0) { if ($pids eq "") { $pids = $curpid; } else { $pids = $pids . "," . $curpid; } $curpid = -1; } } } close(P); wait; if ($pids eq "") { die "$Pn: no processes using $fnm located.\n"; } print "watch_file: $fnm being used by processes:\n\t$pids\n\n"; # Read repeated lsof output from a pipe and display. $pipe = "$LSOF -ap $pids -r $RPT $fnm"; open(P, "$pipe|") || die "$Pn: can't pipe: $pipe\n"; while (<P>) { print $_; } close(P); print "$Pn: unexpected EOF from \"$pipe\"\n"; exit 1; ## isexec($path) -- is $path executable # # $path = absolute or relative path to file to test for executabiity. # Paths that begin with neither '/' nor '.' that arent't found as # simple references are also tested with the path prefixes of the # PATH environment variable. sub isexec { my ($path) = @_; my ($i, @P, $PATH); $path =~ s/^\s+|\s+$//g; if ($path eq "") { return(""); } if (($path =~ m#^[\/\.]#)) { if (-x $path) { return($path); } return(""); } $PATH = $ENV{PATH}; @P = split(":", $PATH); for ($i = 0; $i <= $#P; $i++) { if (-x "$P[$i]/$path") { return("$P[$i]/$path"); } } return(""); }