File Coverage

blib/lib/File/Pid.pm
Criterion Covered Total %
statement 51 51 100.0
branch 9 10 90.0
condition 2 3 66.6
subroutine 13 13 100.0
pod 5 5 100.0
total 80 82 97.5


line stmt bran cond sub pod time code
1             package File::Pid;
2             # $Id: Pid.pm,v 1.1 2005/01/11 13:09:54 cwest Exp $
3 2     2   2724 use strict;
  2         4  
  2         96  
4              
5             =head1 NAME
6              
7             File::Pid - Pid File Manipulation
8              
9             =head1 SYNOPSIS
10              
11             use File::Pid;
12            
13             my $pidfile = File::Pid->new({
14             file => '/some/file.pid',
15             });
16            
17             $pidfile->write;
18            
19             if ( my $num = $pidfile->running ) {
20             die "Already running: $num\n";
21             }
22              
23             $pidfile->remove;
24              
25             =cut
26              
27 2     2   12 use vars qw[$VERSION];
  2         6  
  2         486  
28             $VERSION = sprintf "%d.%02d", split m/\./, (qw$Revision: 1.1 $)[1];
29              
30 2     2   32 use File::Spec::Functions qw[tmpdir catfile];
  2         4  
  2         152  
31 2     2   12 use File::Basename qw[basename];
  2         2  
  2         148  
32 2     2   12 use base qw[Class::Accessor::Fast];
  2         2  
  2         3980  
33              
34             =head1 DESCRIPTION
35              
36             This software manages a pid file for you. It will create a pid file,
37             query the process within to discover if it's still running, and remove
38             the pid file.
39              
40             =head2 new
41              
42             my $pidfile = File::Pid->new;
43              
44             my $thisfile = File::Pid->new({
45             file => '/var/run/daemon.pid',
46             });
47              
48             my $thisfileandpid = File::Pid->new({
49             file => '/var/run/daemon.pid',
50             pid => '145',
51             });
52              
53             This constructor takes two optional paramters.
54              
55             C<file> - The name of the pid file to work on. If not specified, a pid
56             file located in C<< File::Spec->tmpdir() >> will be created that matches
57             C<< (File::Basename::basename($0))[0] . '.pid' >>. So, for example, if
58             C<$0> is F<~/bin/sig.pl>, the pid file will be F</tmp/sig.pl.pid>.
59              
60             C<pid> - The pid to write to a new pidfile. If not specified, C<$$> is
61             used when the pid file doesn't exist. When the pid file does exist, the
62             pid inside it is used.
63              
64             =head2 file
65              
66             my $pidfile = $pidfile->file;
67              
68             Accessor/mutator for the filename used as the pid file.
69              
70             =head2 pid
71              
72             my $pid = $pidfile->pid;
73              
74             Accessor/mutator for the pid being saved to the pid file.
75              
76             =cut
77              
78             sub new {
79 4     4 1 310024 my $class = shift;
80 4         107 my $self = $class->SUPER::new(@_);
81 4         128 $self->_get_pidfile;
82 4         75 $self->_get_pid;
83 4         62 return $self;
84             }
85             __PACKAGE__->mk_accessors(qw[file pid]);
86              
87             =head2 write
88              
89             my $pid = $pidfile->write;
90              
91             Writes the pid file to disk, inserting the pid inside the file.
92             On success, the pid written is returned. On failure, C<undef> is
93             returned.
94              
95             =cut
96              
97             sub write {
98 3     3 1 3300 my $self = shift;
99 3         11 my $file = $self->_get_pidfile;
100 3         39 my $pid = $self->_get_pid;
101              
102 3         41 local *WRITEPID;
103 3 50       909 open WRITEPID, "> $file" or return;
104 3         84 print WRITEPID "$pid\n";
105 3         435 close WRITEPID;
106 3         388 return $pid;
107             }
108              
109             =head2 running
110              
111             my $pid = $pidfile->running;
112             die "Service already running: $pid\n" if $pid;
113              
114             Checks to see if the pricess identified in the pid file is still
115             running. If the process is still running, the pid is returned. Otherwise
116             C<undef> is returned.
117              
118             =cut
119              
120             sub running {
121 3     3 1 1382 my $self = shift;
122 3         12 my $pid = $self->_get_pid_from_file;
123              
124 3 100       251 return kill(0, $pid)
125             ? $pid
126             : undef;
127             }
128              
129             =head2 remove
130              
131             $pidfile->remove or warn "Couldn't unlink pid file\n";
132              
133             Removes the pid file from disk. Returns true on success, false on
134             failure.
135              
136             =cut
137              
138 3     3 1 13 sub remove { unlink shift->_get_pidfile }
139              
140             =head2 program_name
141              
142             This is a utility method that allows you to determine what
143             C<File::Pid> thinks the program name is. Internally this is used
144             when no pid file is specified.
145              
146             =cut
147              
148             sub program_name {
149 4     4 1 15262 my $self = shift;
150 4         252 my ($name) = basename($0);
151 4         34 return $name;
152             }
153              
154             sub _get_pidfile {
155 16     16   27 my $self = shift;
156 16 100       68 return $self->file if $self->file;
157              
158 2         52 my $file = catfile tmpdir, $self->program_name . '.pid';
159 2         12 $self->file($file);
160 2         20 return $self->file;
161             }
162              
163             sub _get_pid {
164 7     7   12 my $self = shift;
165 7 100       30 return $self->pid if $self->pid;
166 3   66     697 $self->pid($self->_get_pid_from_file || $$);
167 3         35 return $self->pid;
168             }
169              
170             sub _get_pid_from_file {
171 6     6   11 my $self = shift;
172 6         166 my $file = $self->_get_pidfile;
173 6         80 local *READPID;
174 6 100       413 open READPID, "< $file" or return;
175 4         100 chomp(my $pid = <READPID>);
176 4         46 close READPID;
177 4         21 return $pid;
178             }
179              
180             1;
181              
182             __END__
183              
184             =head1 SEE ALSO
185              
186             L<perl>.
187              
188             =head1 AUTHOR
189              
190             Casey West, <F<casey@geeknest.com>>.
191              
192             =head1 COPYRIGHT
193              
194             Copyright (c) 2005 Casey West. All rights reserved.
195             This module is free software; you can redistribute it and/or modify it
196             under the same terms as Perl itself.
197              
198             =cut