forked from skx/sysadmin-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
with-lock
executable file
·198 lines (127 loc) · 2.77 KB
/
with-lock
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env perl
=head1 NAME
with-lock - Prevent multiple executions of a program
=cut
=head1 SYNOPSIS
General Options:
--help Show the help information for this script.
--verbose Show useful debugging information.
=cut
=head1 ABOUT
with-lock allows you to run a command under an execlusive
lock - this will prevent you from running two identical
commands at the same time.
The program is similar to lckdo but automatically determines
the name of the lockfile to use by hashing the specified command.
=cut
=head1 AUTHOR
Steve
--
http://www.steve.org.uk/
=cut
=head1 LICENSE
Copyright (c) 2013 by Steve Kemp. All rights reserved.
This script is free software;you can redistribute it and/or modify it under
the same terms as Perl itself.
The LICENSE file contains the full text of the license.
=cut
use strict;
use warnings;
use English;
use Fcntl qw(:DEFAULT :flock);
use Getopt::Long;
use Pod::Usage;
#
# Get the options, either defaults or from the command line.
#
my %config = parsedOptions();
#
# Ensure we have arguments
#
if ( $#ARGV < 0 )
{
print "Usage: $0 command to execute\n";
exit(1);
}
#
# Sum up the arguments we have
#
my $hash = sumArgs(@ARGV);
#
# The lockfile we'll use
#
my $lock = "/var/tmp/$UID.$hash";
#
# Open it.
#
open( my $handle, ">", $lock ) or
die "Failed to open lockfile $lock - $!";
#
# Lock it, aborting on error.
#
unless ( flock( $handle, LOCK_EX | LOCK_NB ) )
{
print "Failed to acquire lock\n";
if ( $config{ 'verbose' } )
{
print "Lockfile is $lock\n";
}
exit(1);
}
#
# Run our command.
#
my $ret = system(@ARGV);
#
# Cleanup
#
close($handle);
unlink($lock) if ($lock);
exit($ret);
=begin doc
Parse the command-line options.
=end doc
=cut
sub parsedOptions
{
my %vars;
exit
if (
!GetOptions( "help" => \$vars{ 'help' },
"verbose" => \$vars{ 'verbose' } ) );
pod2usage(1) if ( $vars{ 'help' } );
return (%vars);
}
=begin doc
Checksum the given array (== command line program + args ) and return
the SHA1 digest of them
=end doc
=cut
sub sumArgs
{
my (@args) = (@_);
my $hash = undef;
foreach my $module (qw! Digest::SHA Digest::SHA1 !)
{
# If we succeeded in calculating the hash we're done.
next if ( defined($hash) );
# Attempt to load the module
my $eval = "use $module;";
## no critic (Eval)
eval($eval);
## use critic
#
# Loaded module, with no errors.
#
if ( !$@ )
{
my $object = $module->new;
foreach my $a (@args)
{
$object->add($a);
}
$hash = $object->hexdigest();
}
}
return ($hash);
}