blib/lib/Piddy.pm

filter: Statementstmt 36.7% 47 / 128 Branchbran 13.4% 7 / 52 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 9 / 18 Podpod 90.0% 9 / 10 totaltotal 33.0% 72 / 218 CC 30 SCAR 29.4
CC
30
Cov
29%
new
47.7
running
30.0
read
20.0
CRAP 19.0
↑ Summary
1 package Piddy;
2  
3 =head1 NAME
4  
5 Piddy - Easy Linux PID Management
6  
7 =head1 DESCRIPTION
8  
9 Manage the current process/pid and/or external ones (Not the current process) easily with this module. Use
10 it to create helpful sysadmin scripts while it lets you control the flow of a process by suspending and resuming
11 it at will. Some options require root access, but Piddy will let you know which ones when you try to run them.
12 Piddy will even attempt to determine if the pid instance is actually running as a threaded process.
13 This module probably still needs a lot of work, but it functions fine for the most part.
14  
15 =head1 SYNOPSIS
16  
17 use Piddy;
18  
19 my $pid = Piddy->new({
20 pid => 5367,
21 path => '/var/run/pids',
22 });
23  
24 if ($pid->running($pid->pid)) {
25 $pid->suspend($pid->pid); # temporarily stop the process where it is
26 print $pid->info('state') . "\n"; # read the current state of the process
27 sleep 20;
28 $pid->continue($pid->pid); # resume the process from where it was stopped
29 }
30 else { print "Oh.. " . $pid->pid . " is not actually running..\n"; }
31  
32 =cut
33  
34 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 2 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use File::Basename 'fileparse';
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 2 $Piddy::VERSION = '0.02';
42  
43 =head2 new
44  
45 Creates a new PID instance. There are a couple of options you can pass...
46  
47 pid = Use an external PID (Not the current running process).
48 path = Location of the pid file
49  
50 # Use pid 5367 and save the pid file as /var/run/pids/5367.pid
51 my $p = Piddy->new({pid => 5367, path => '/var/run/pids'});
52  
53 =cut
54  
55 1 ▶sub new {
Subroutine new: called CC=11 SCAR=38.7
Pod documented
56 1 my ($class, $args) = @_;
57
58 1 my $ext_pid = 0; # are we using a pid other than the current running script?
59 1 my $self = {};
60 1 my ($name, $path, $suffix) = fileparse($0, '\.[^\.]*');
61 1 $self->{path} = "$FindBin::Bin";
62 1 $self->{errors} = [];
63 1 ▶ if ($args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) 1 0 1
64 1 for (keys %$args) {
65 1 given ($_) {
66 1 when ('path') {
67 0 $self->{path} = $args->{$_};
68 }
69 1 when ('pid') {
70 1 $self->{pid} = $args->{$_};
71 1 $ext_pid = 1;
72 }
73 0 default {
74 0 warn "Unknown option: $_";
75 0 __PACKAGE__->last_error("Unknown option: $_");
76 }
77 }
78 }
79 }
80  
81 1 bless $self, __PACKAGE__;
82 1 ▶ if (! $ext_pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ext_pid) { } 0 1 1
83 0 my $filename = "$self->{path}/$name.pid";
84 0 $self = {
85 pid => $$,
86 pid_file => $filename,
87 };
88  
89 0 $self->_read_info;
90  
91 0 ▶ if ($self->_pid_exists($filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_pid_exists($filename)) 0 0 0
92 # we may be inside a thread if the pid is still running
93 # check to see if it's the ppid, if not, kill it.
94 0 my $rpid = $self->read($filename);
95 0 ▶ if (! $rpid eq getppid()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!$rpid eq getppid) { } 0 0 0
96 # remove it nicely, but if that fails, then DESTROY IT
97 0 kill 15, $rpid;
98 0 ▶ if ($self->running) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->running) 0 0 0
99 0 kill 9, $rpid;
100 0 ▶ if ($self->running) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->running) 0 0 0
101 0 warn "Argh. I could not kill $rpid... can you please do it for me? :-(";
102 }
103 }
104
105 0 unlink $filename;
106 }
107 else {
108 # need to change filename to reflect the forked process
109 0 ▶ my $path = $self->{path}||"$FindBin::Bin";
Condition0%0/3
$self->{'path'} || "$FindBin::Bin"
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'path'} || "$FindBin::Bin"
$self->{'path'} "$FindBin::Bin"
Truth table0%0/3
A || B
A= $self->{'path'}B= "$FindBin::Bin"
ABresult
1X1
011
000
110 0 $filename = $path . "/" . $name . "." . $self->{pid} . ".pid";
111 0 $self->{pid_file} = $filename;
112 }
113 }
114 0 ▶ open(my $pid_file, ">$filename") or
Branch: 1 branch
BranchTrueFalseTotal
open my $pid_file, ">$filename" or die "Could not open pid file $filename for writing" 0 0 0
115 die "Could not open pid file $filename for writing";
116  
117 0 ▶ print $pid_file $$ or do {
Branch: 1 branch
BranchTrueFalseTotal
print $pid_file $$ or do { $self->last_error("Could not write PID to $filename") } 0 0 0
118 0 $self->last_error("Could not write PID to $filename");
119 };
120
121 0 close $pid_file;
122 0 return $self;
123 }
124 else {
125 1 $self->{extpid} = 1;
126 1 $self->_read_info;
127 1 return $self;
128 }
129 }
130  
131 =head2 info
132  
133 Reads information on the process from /proc
134  
135 my $state = $pid->info('state'); # Piddy formats state to make it look nicer, too!
136  
137 =cut
138  
139 0 ▶sub info {
Subroutine info: not called CC=2 SCAR=17.9
Pod documented
140 0 my ($self, $info) = @_;
141  
142 0 $self->_read_info;
143 0 ▶ if (exists $self->{info}->{$info}) { return $self->{info}->{$info}; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'info'}{$info}) { } 0 0 0
144 0 else { return 0; }
145 }
146  
147 =head2 suspend
148  
149 Temporarily suspend a process (will not kill it, simply stops it exactly where it is so you
150 can resume it later. Handy when writing scripts to monitor performance - you can stop the process
151 then resume it when things have cooled down.
152  
153 $pid->suspend(5367);
154  
155 =cut
156  
157 0 ▶sub suspend {
Subroutine suspend: not called CC=2 SCAR=17.9
Pod documented
158 0 my ($self, $pid) = @_;
159  
160 0 ▶ if ($self->kill('-STOP', $pid)) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->kill('-STOP', $pid)) { } 0 0 0
161 0 else { return 0; }
162 }
163  
164 =head2 continue
165  
166 Resumes a stopped process.
167  
168 $pid->continue(5367);
169  
170 =cut
171  
172 0 ▶sub continue {
Subroutine continue: not called CC=2 SCAR=17.9
Pod documented
173 0 my ($self, $pid) = @_;
174  
175 0 ▶ if ($self->kill('-CONT', $pid)) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->kill('-CONT', $pid)) { } 0 0 0
176 0 else { return 0; }
177 }
178  
179 =head2 kill
180  
181 Uses the systems kill command instead of Perl's. If you simply want to -9 or -15
182 a process then use Perl, but for things like stopping/continuing processes, I could
183 not get it to work any other way.
184  
185 $pid->kill('-9', 5367);
186 $pid->kill('-STOP', 5367); # or just use $pid->suspend(5367);
187  
188 =cut
189  
190 0 ▶sub kill {
Subroutine kill: not called CC=3 SCAR=24.8
Pod documented
191 0 my ($self, $args, $pid) = @_;
192  
193 0 ▶ if ($< != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($< != 0) 0 0 0
194 0 warn "This action requires root access";
195 0 return 0;
196 }
197
198 0 my $cmd = `kill $args $pid`;
199 0 chomp $cmd;
200 0 ▶ if ($cmd eq '') { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq '') { } 0 0 0
201 0 else { return 0; }
202 }
203  
204 =head2 ppid
205  
206 Returns the parent process id
207  
208 =cut
209  
210 0 ▶sub ppid {
Subroutine ppid: not called CC=1 SCAR=6.9
Pod documented
211 0 my $self = shift;
212
213 0 return getppid();
214 }
215  
216 =head2 pid
217  
218 Returns the pid of the current instance
219  
220 =cut
221  
222 1 ▶sub pid {
Subroutine pid: called CC=1 SCAR=0.0
Pod documented
223 1 my $self = shift;
224
225 1 return $self->{pid};
226 }
227  
228 =head2 running
229  
230 Determines whether the current pid is running, or if you pass
231 another pid as an argument it will check that instead.
232 By default it will use /proc, otherwise it will revert to ps and grep.
233  
234 if ($pid->running(5367)) { print "It's running!\n"; }
235  
236 =cut
237  
238 0 ▶sub running {
Subroutine running: not called CC=5 SCAR=34.0
Pod documented
239 0 my ($self, $cpid) = @_;
240  
241 # if the fs has /proc, then use it
242 # otherwise fallback on ps
243 0 ▶ my $pid = $cpid||$self->{pid};
Condition0%0/3
$cpid || $self->{'pid'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$cpid || $self->{'pid'}
$cpid $self->{'pid'}
Truth table0%0/3
A || B
A= $cpidB= $self->{'pid'}
ABresult
1X1
011
000
244 0 ▶ if (-d '/proc') {
Branch: 1 branch
BranchTrueFalseTotal
if (-d '/proc') { } 0 0 0
245 0 ▶ if (-d "/proc/$pid") {
Branch: 1 branch
BranchTrueFalseTotal
if (-d "/proc/$pid") { } 0 0 0
246 0 return 1;
247 }
248 0 else { return 0; }
249 }
250 else {
251 0 my $ps = `ps -A | grep $pid`;
252 0 $ps =~ s/^\s*//;
253 0 ▶ if ($ps =~ /^$pid /) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($ps =~ /^$pid /) { } 0 0 0
254 0 else { return 0; }
255 }
256 }
257  
258 =head2 last_error
259  
260 Returns the last known error
261  
262 =cut
263  
264 0 ▶sub last_error {
Subroutine last_error: not called CC=2 SCAR=17.9
Pod documented
265 0 my ($self, $err) = @_;
266  
267 0 ▶ if ($err) { push @{$self->{errors}}, $err }
Branch: 1 branch
BranchTrueFalseTotal
if ($err) { } 0 0 0
268 0 else { return $self->{errors}->[ scalar(@{$self->{errors}})+1 ]; }
269 }
270  
271 0 ▶sub read {
Subroutine read: not called CC=4 SCAR=30.0
Pod undocumented
272 0 my ($self, $fname) = @_;
273  
274 0 ▶ if (! $self->_pid_exists($fname)) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_pid_exists($fname)) { } 0 0 0
275 else {
276 0 my $getpid;
277 0 ▶ open (my $pid, "<$fname") or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $pid, "<$fname" or return 0 0 0 0
278 0 ▶ while(<$pid>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $pid)) 0 0 0
279 0 $getpid = $_;
280 }
281 0 close $pid;
282
283 0 return $getpid;
284 }
285 }
286  
287 0 ▶sub _pid_exists {
Subroutine _pid_exists: not called CC=2 SCAR=17.9
288 0 my ($self, $fname) = @_;
289  
290 0 ▶ if (-f $fname) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (-f $fname) { } 0 0 0
291 0 else { return 0; }
292 }
293  
294 1 ▶sub _read_info {
Subroutine _read_info: called CC=4 SCAR=14.0
295 1 my $self = shift;
296  
297 1 ▶ open(my $proc, "/proc/$self->{pid}/status") or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $proc, qq[/proc/$self->{"pid"}/status] or return 0 1 0 1
298 1 ▶ while(<$proc>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $proc)) 61 1 62
299 61 my ($key, $value) = /(.+):\s*(.+)/;
300 61 $self->{info}->{lc($key)} = $value;
301 }
302 1 close $proc;
303  
304 # some fixups
305 # state
306 1 my $state = $self->{info}->{state};
307 1 ▶ if ($state =~ /(.+)\s*\((.+)\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state =~ /(.+)\s*\((.+)\)/) 1 0 1
308 1 $self->{info}->{state} = lc $2;
309 }
310 }
311  
312 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=17.3
313 1 my $self = shift;
314 1 ▶ if (! $self->{extpid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"extpid"}) 0 1 1
315 0 ▶ if (! unlink $self->{pid_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not unlink($self->{'pid_file'})) { } 0 0 0
316 0 warn "There was a problem removing '$self->{pid_file}'";
317 }
318 0 else { say "Removed $self->{pid_file}"; }
319 }
320 }
321  
322 =head1 BUGS
323  
324 Please e-mail bradh@cpan.org
325  
326 =head1 AUTHOR
327  
328 Brad Haywood <bradh@cpan.org>
329  
330 =head1 COPYRIGHT & LICENSE
331  
332 Copyright 2011 the above author(s).
333  
334 This sofware is free software, and is licensed under the same terms as perl itself.
335  
336 =cut
337  
338 2 1;
↑ Summary