-
Notifications
You must be signed in to change notification settings - Fork 7
/
run-lock.pl
executable file
·41 lines (33 loc) · 1.22 KB
/
run-lock.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/perl -w
sub datestr {
$str = `date`;
$str =~ s/\s+$//sg;
return "$str ";
}
$lockdir = "locks";
if (not -d $lockdir) {
system("mkdir $lockdir");
}
die "FATAL: '$lockdir' is not a directory!" unless -d $lockdir;
$cmd = join(' ', @ARGV);
$lockcmd = $cmd;
$lockcmd =~ s/\s\s+/ /g;
while ($lockcmd =~ m/[\s\.\/\\]/) { $lockcmd =~ s/[\s\.\/\\]/_/g; }
$lockfile = "$lockdir/lock-$lockcmd";
if (-e $lockfile) {
print STDERR datestr(), "Existing lock file:\t'$lockfile'\n";
print STDERR datestr(), "Skipping command:\t'$cmd'\n";
exit;
} else {
system("touch $lockfile");
die "FATAL: Could not create lock file: '$lockfile'\n" unless -e $lockfile;
print STDERR datestr(), "Created lock file:\t'$lockfile'\n";
print STDERR datestr(), "Running command:\t'$cmd'\n";
system("nice -10 time $cmd && rm $lockfile");
print STDERR datestr(), "Command terminated:\t'$cmd'\n";
if (-e $lockfile) {
print STDERR datestr(), "Lock file still present. Forcing removal...\n";
system("rm -f $lockfile");
die "FATAL: Could not remove lock file: '$lockfile'\n" unless not -e $lockfile;
}
}