你如何使用锁定文件的locktimeout选项?

时间:2014-01-23 23:00:31

标签: perl unix locking lockfile

从lockfile手册页:

  

如果指定了locktimeout,则locktimeout秒后将强制删除锁定文件         自上次修改/创建锁定文件以来已经过去了(很可能是其他一些意外的程序 -         edly在很久以前去世了,因此无法清理任何剩余的锁文件。)

附加脚本的输出:

locktimeout: Lockfile created with lockout time of 12
locktimeout: Will sleep for 20 seconds, expecting lockfile to be removed by then...
locktimeout: Lockfile not removed after 20 seconds

剧本:

my $pName = $0;     
$pName =~ s/.*\///; 
$pName =~ s/\.\w+$//;
my $LOG = undef;    
my $lockouttime = 12;
my $sleeper = 20;

chomp (my $wd = `pwd`);
chomp (my $timeNow = `date +"%Y-%m-%d-%H-%M-%S"`);
my $lockFile = $wd . '/' . $pName . '.lock';

sub complain {
        print "$pName: Error: Cannot acquire lock: $!\n";
        print "$pName: Aborting at $timeNow\n";
        exit 1
}

print "$pName: lockfile not removed after $sleeper seconds\n" if (-f $lockFile);
system "lockfile -l $lockouttime -r 0 $lockFile";
complain if ($?);
print "$pName: Lockfile created with lockout time of $lockouttime\n";
print "$pName: Will sleep for $sleeper seconds, expecting lockfile to be removed by then...\n";
sleep $sleeper;
print "$pName: Lockfile not removed after $sleeper seconds\n" if (-f $lockFile);
exit 0;

0 个答案:

没有答案