line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
package Proc::PidUtil; |
3
|
|
|
|
|
|
|
|
4
|
2
|
|
|
2
|
|
1278
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
76
|
|
5
|
|
|
|
|
|
|
#use diagnostics; |
6
|
2
|
|
|
2
|
|
10
|
use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1200
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require Exporter; |
9
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$VERSION = do { my @r = (q$Revision: 0.09 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
%EXPORT_TAGS = ( |
14
|
|
|
|
|
|
|
all => [qw( |
15
|
|
|
|
|
|
|
is_running |
16
|
|
|
|
|
|
|
make_pidfile |
17
|
|
|
|
|
|
|
zap_pidfile |
18
|
|
|
|
|
|
|
get_script_name |
19
|
|
|
|
|
|
|
if_run_exit |
20
|
|
|
|
|
|
|
)], |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
Exporter::export_ok_tags('all'); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 NAME |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Proc::PidUtil - PID file management utilities |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 SYNOPSIS |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
use Proc::PidUtil qw( |
32
|
|
|
|
|
|
|
if_run_exit |
33
|
|
|
|
|
|
|
is_running |
34
|
|
|
|
|
|
|
make_pidfile |
35
|
|
|
|
|
|
|
zap_pidfile |
36
|
|
|
|
|
|
|
get_script_name |
37
|
|
|
|
|
|
|
:all |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=cut |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 DESCRIPTION |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
B provides utilities to manage PID files |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=over 2 |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=item * $rv = if_run_exit('path',$message); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
This routine checks for a file named: |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
'(scriptname).pid |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
in the the $path directory. If a file is found and the PID found in the file |
55
|
|
|
|
|
|
|
is currently a running job, there is no return, the subroutine prints |
56
|
|
|
|
|
|
|
the (scriptname): $pid, $message to STDERR and exits. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
If there is no file or the PID does not match a running job, run_exit |
59
|
|
|
|
|
|
|
returns true. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
input: path for pidfiles |
62
|
|
|
|
|
|
|
return: true if not running |
63
|
|
|
|
|
|
|
else exits |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Note: also exits if $path is false |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=cut |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub if_run_exit { |
70
|
0
|
|
|
0
|
1
|
0
|
my($path,$message) = @_; |
71
|
0
|
|
|
|
|
0
|
my $me = get_script_name(); |
72
|
0
|
0
|
|
|
|
0
|
unless ($path) { |
73
|
0
|
|
|
|
|
0
|
print STDERR "$me: $path not found\n"; |
74
|
0
|
|
|
|
|
0
|
exit; |
75
|
|
|
|
|
|
|
} |
76
|
0
|
0
|
|
|
|
0
|
unless (-w $path) { |
77
|
0
|
|
|
|
|
0
|
print STDERR "$me: $path not writable, check permissions\n"; |
78
|
0
|
|
|
|
|
0
|
exit; |
79
|
|
|
|
|
|
|
} |
80
|
0
|
|
|
|
|
0
|
my $pidfile = $path .'/'. $me . '.pid'; |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
0
|
my $job = is_running($pidfile); |
83
|
0
|
0
|
|
|
|
0
|
if ($job) { |
84
|
0
|
0
|
|
|
|
0
|
print STDERR "$me: $job, $message\n" |
85
|
|
|
|
|
|
|
if $message; |
86
|
0
|
|
|
|
|
0
|
exit; |
87
|
|
|
|
|
|
|
} |
88
|
0
|
|
|
|
|
0
|
make_pidfile($pidfile); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=item * $rv = is_running('path2pidfile'); |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Check that the job described by the pid file is running. |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
input: path 2 pid file |
96
|
|
|
|
|
|
|
returns: pid or false (0) if not running |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=cut |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub is_running { |
101
|
4
|
|
|
4
|
1
|
296
|
my($pidfile) = @_; |
102
|
4
|
|
|
|
|
10
|
local *PID; |
103
|
4
|
50
|
66
|
|
|
264
|
return 0 unless |
|
|
|
66
|
|
|
|
|
104
|
|
|
|
|
|
|
-e $pidfile && |
105
|
|
|
|
|
|
|
-r $pidfile && |
106
|
|
|
|
|
|
|
open(PID,$pidfile); |
107
|
3
|
|
50
|
|
|
44
|
local $_ = || 0; # get pid |
108
|
3
|
|
|
|
|
25
|
close PID; |
109
|
3
|
|
|
|
|
6
|
chomp; |
110
|
3
|
|
|
|
|
31
|
/^\d+$/; |
111
|
3
|
|
|
|
|
10
|
$_ = $&; |
112
|
3
|
50
|
33
|
|
|
30
|
return 0 unless $_ && $_ !~ /\D/; # skip bogus pid files |
113
|
3
|
100
|
|
|
|
62
|
return (kill 0, $_) ? $_ : 0; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=item * $rv = make_pidfile('path2pidfile',$pid); |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Open a pid file and insert the pid value. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
input: path 2 pid file, |
121
|
|
|
|
|
|
|
pid value || $$ |
122
|
|
|
|
|
|
|
returns: pid or false (0) on error |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=cut |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub make_pidfile { |
127
|
3
|
|
|
3
|
1
|
204591
|
my($pidfile,$pid) = @_; |
128
|
3
|
100
|
|
|
|
22
|
$pid = $$ unless $pid; |
129
|
3
|
|
|
|
|
20
|
local *PID; |
130
|
3
|
50
|
|
|
|
304
|
return 0 unless open(PID,'>'.$pidfile); |
131
|
3
|
|
|
|
|
50
|
print PID $pid,"\n"; |
132
|
3
|
|
|
|
|
140
|
close PID; |
133
|
3
|
|
|
|
|
25
|
return $pid; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item * $rv = zap_pidfile($path); |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
input: path for pidfiles |
139
|
|
|
|
|
|
|
returns: return value of 'unlink' |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=cut |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub zap_pidfile { |
144
|
1
|
|
|
1
|
1
|
19
|
my ($path) = @_; |
145
|
1
|
|
|
|
|
9
|
my $me = get_script_name(); |
146
|
1
|
|
|
|
|
7
|
my $pidfile = $path .'/'. $me . '.pid'; |
147
|
1
|
|
|
|
|
92
|
unlink $pidfile; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item * $me = get_script_name(); |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
This function returns the script name portion of the path found in $0; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
input: none |
155
|
|
|
|
|
|
|
returns: script name |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
i.e. if the script name is: |
158
|
|
|
|
|
|
|
/usr/local/stuff/scripts/sc_admin.pl |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
$me = get_script_name(); |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
returns ('sc_admin.pl') |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=back |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=cut |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub get_script_name { |
169
|
3
|
|
|
3
|
1
|
846
|
$0 =~ m|[^/]+$|; |
170
|
3
|
|
|
|
|
15
|
return $&; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
none |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 EXPORT_OK |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
if_run_exit |
180
|
|
|
|
|
|
|
is_running |
181
|
|
|
|
|
|
|
make_pidfile |
182
|
|
|
|
|
|
|
zap_pidfile |
183
|
|
|
|
|
|
|
get_script_name |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head1 EXPORT_TAGS |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
:all |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head1 COPYRIGHT |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Copyright 2003 -2014, Michael Robinton |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify |
194
|
|
|
|
|
|
|
it under the terms of the GNU General Public License as published by |
195
|
|
|
|
|
|
|
the Free Software Foundation; either version 2 of the License, or |
196
|
|
|
|
|
|
|
(at your option) any later version. |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, |
199
|
|
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
200
|
|
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
201
|
|
|
|
|
|
|
GNU General Public License for more details. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License |
204
|
|
|
|
|
|
|
along with this program; if not, write to the Free Software |
205
|
|
|
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head1 AUTHOR |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Michael Robinton |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=cut |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
1; |