| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | package Proc::PidUtil; | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | #use diagnostics; | ||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | $VERSION = do { my @r = (q$Revision: 0.09 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | %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 | 2 | 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<Proc::PidUtil> 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 | 0 | ▶ | sub if_run_exit { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
if_run_exit: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my($path,$message) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my $me = get_script_name(); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | unless ($path) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | print STDERR "$me: $path not found\n"; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | exit; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | unless (-w $path) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | print STDERR "$me: $path not writable, check permissions\n"; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | exit; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $pidfile = $path .'/'. $me . '.pid'; | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my $job = is_running($pidfile); | |||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ($job) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | print STDERR "$me: $job, $message\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | if $message; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | exit; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 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 | 4 | ▶ | sub is_running { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_running: called CC=8 SCAR=21.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 4 | my($pidfile) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 4 | local *PID; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 4 | ▶ | return 0 unless | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
-e $pidfile and -r $pidfile and open PID, $pidfile
-e $pidfile and -r $pidfile
MC/DC33%1/3
-e $pidfile and -r $pidfile and open PID, $pidfile
-e $pidfile
-r $pidfile
open PID, $pidfile
Truth table50%2/4
A and B and C
A= -e $pidfileB= -r $pidfileC= open PID, $pidfile
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | -e $pidfile && | ||||||||||||||||||||||||||||||||||||||||||
| 105 | -r $pidfile && | ||||||||||||||||||||||||||||||||||||||||||
| 106 | open(PID,$pidfile); | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 3 | ▶ | local $_ = <PID> || 0; # get pid | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
readline PID || 0
MC/DC0%0/1
readline PID || 0
readline PID
Truth table50%1/2
A
A= readline PID
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 3 | close PID; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 3 | chomp; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 3 | /^\d+$/; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | $_ = $&; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | ▶ | return 0 unless $_ && $_ !~ /\D/; # skip bogus pid files | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_ and not $_ =~ /\D/
MC/DC0%0/2
$_ and not $_ =~ /\D/
$_
not $_ =~ /\D/
Truth table33%1/3
A and not(B)
A= $_B= not $_ =~ /\D/
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | ▶ | return (kill 0, $_) ? $_ : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 3 | ▶ | sub make_pidfile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
make_pidfile: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 3 | my($pidfile,$pid) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 3 | ▶ | $pid = $$ unless $pid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 3 | local *PID; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 3 | ▶ | return 0 unless open(PID,'>'.$pidfile); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 3 | print PID $pid,"\n"; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 3 | close PID; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 3 | 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 | 1 | ▶ | sub zap_pidfile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
zap_pidfile: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | my ($path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | my $me = get_script_name(); | |||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | my $pidfile = $path .'/'. $me . '.pid'; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | 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 | 3 | ▶ | sub get_script_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_script_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 3 | $0 =~ m|[^/]+$|; | |||||||||||||||||||||||||||||||||||||||||
| 170 | 3 | 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 <michael@bizsystems.com> | ||||||||||||||||||||||||||||||||||||||||||
| 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 <michael@bizsystems.com> | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||