--- kvake.pl 2000/04/14 07:24:06 1.4 +++ kvake.pl 2000/05/03 07:21:50 1.5 @@ -13,12 +13,16 @@ # 2000-03-01 DbP poravljen ignore_dirs grep bug # # 2000-03-31 Hana sada ignorira sve sto ^eng$, ^eng_ , ^adresar$ u imenu direktorija +# 2000-05-03 DbP lock support $mod_time = 10*60; # 10 minuta #$mod_time = 24*60*60; # 1 day $verbose = 0; +use Fcntl ':flock'; # import LOCK_* +$lock="/tmp/kvake.lock"; + #@ignore_dirs=( "/data/web/public/adresar/", "/data/web/public/eng" ); while (defined($ARGV[0]) && $ARGV[0] eq "-v") { @@ -36,6 +40,32 @@ print STDERR "Scanning dirs: @dirs\n" if ($verbose); +print STDERR "locking\n" if ($verbose); +while (-e "$lock") { + sub read_lock_pid { + my $pid; + open(LOCK,"$lock") || die "can't open lock $lock: $!"; + $pid=; + close(LOCK); + return $pid; + } + $run_pid=read_lock_pid; + print STDERR "lock on pid $run_pid found\n"; + # if not over in 15 min. kill (next re-run will do actual work) + sleep 15*60; + $new_pid=read_lock_pid; + if ($run_pid == $new_pid) { + print STDERR "killing process $run_pid and clearing lock\n"; + kill 9,$run_pid; + unlink $lock; + die; + } +} + +open(LOCK,">$lock") || die "can't open lock $lock: $!"; +print LOCK "$$"; +flock(LOCK,LOCK_EX); + foreach $dir (@dirs) { opendir(DIR,"$dir") || warn "can't open $dir: $!"; foreach $f (grep { !/^\./ } readdir(DIR)) { @@ -86,6 +116,11 @@ chown $stat[4],$stat[5],"$file"; } +print STDERR "unlocking\n" if ($verbose); +flock(LOCK,LOCK_UN); +close(LOCK); +unlink $lock; + 1; sub replace {