File-Pid-1.01/0000755000076500000000000000000010170747403013272 5ustar cwestwheel00000000000000File-Pid-1.01/Changes0000644000076500000000000000005110170747140014557 0ustar cwestwheel000000000000001.01 2005-01-11 - Initial version. File-Pid-1.01/lib/0000755000076500000000000000000010170747403014040 5ustar cwestwheel00000000000000File-Pid-1.01/lib/File/0000755000076500000000000000000010170747403014717 5ustar cwestwheel00000000000000File-Pid-1.01/lib/File/Pid.pm0000644000076500000000000000765210170750042015774 0ustar cwestwheel00000000000000package File::Pid; # $Id: Pid.pm,v 1.1 2005/01/11 13:09:54 cwest Exp $ use strict; =head1 NAME File::Pid - Pid File Manipulation =head1 SYNOPSIS use File::Pid; my $pidfile = File::Pid->new({ file => '/some/file.pid', }); $pidfile->write; if ( my $num = $pidfile->running ) { die "Already running: $num\n"; } $pidfile->remove; =cut use vars qw[$VERSION]; $VERSION = sprintf "%d.%02d", split m/\./, (qw$Revision: 1.1 $)[1]; use File::Spec::Functions qw[tmpdir catfile]; use File::Basename qw[basename]; use base qw[Class::Accessor::Fast]; =head1 DESCRIPTION This software manages a pid file for you. It will create a pid file, query the process within to discover if it's still running, and remove the pid file. =head2 new my $pidfile = File::Pid->new; my $thisfile = File::Pid->new({ file => '/var/run/daemon.pid', }); my $thisfileandpid = File::Pid->new({ file => '/var/run/daemon.pid', pid => '145', }); This constructor takes two optional paramters. C - The name of the pid file to work on. If not specified, a pid file located in C<< File::Spec->tmpdir() >> will be created that matches C<< (File::Basename::basename($0))[0] . '.pid' >>. So, for example, if C<$0> is F<~/bin/sig.pl>, the pid file will be F. C - The pid to write to a new pidfile. If not specified, C<$$> is used when the pid file doesn't exist. When the pid file does exist, the pid inside it is used. =head2 file my $pidfile = $pidfile->file; Accessor/mutator for the filename used as the pid file. =head2 pid my $pid = $pidfile->pid; Accessor/mutator for the pid being saved to the pid file. =cut sub new { my $class = shift; my $self = $class->SUPER::new(@_); $self->_get_pidfile; $self->_get_pid; return $self; } __PACKAGE__->mk_accessors(qw[file pid]); =head2 write my $pid = $pidfile->write; Writes the pid file to disk, inserting the pid inside the file. On success, the pid written is returned. On failure, C is returned. =cut sub write { my $self = shift; my $file = $self->_get_pidfile; my $pid = $self->_get_pid; local *WRITEPID; open WRITEPID, "> $file" or return; print WRITEPID "$pid\n"; close WRITEPID; return $pid; } =head2 running my $pid = $pidfile->running; die "Service already running: $pid\n" if $pid; Checks to see if the pricess identified in the pid file is still running. If the process is still running, the pid is returned. Otherwise C is returned. =cut sub running { my $self = shift; my $pid = $self->_get_pid_from_file; return kill(0, $pid) ? $pid : undef; } =head2 remove $pidfile->remove or warn "Couldn't unlink pid file\n"; Removes the pid file from disk. Returns true on success, false on failure. =cut sub remove { unlink shift->_get_pidfile } =head2 program_name This is a utility method that allows you to determine what C thinks the program name is. Internally this is used when no pid file is specified. =cut sub program_name { my $self = shift; my ($name) = basename($0); return $name; } sub _get_pidfile { my $self = shift; return $self->file if $self->file; my $file = catfile tmpdir, $self->program_name . '.pid'; $self->file($file); return $self->file; } sub _get_pid { my $self = shift; return $self->pid if $self->pid; $self->pid($self->_get_pid_from_file || $$); return $self->pid; } sub _get_pid_from_file { my $self = shift; my $file = $self->_get_pidfile; local *READPID; open READPID, "< $file" or return; chomp(my $pid = ); close READPID; return $pid; } 1; __END__ =head1 SEE ALSO L. =head1 AUTHOR Casey West, >. =head1 COPYRIGHT Copyright (c) 2005 Casey West. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut File-Pid-1.01/Makefile.PL0000644000076500000000000000064010170747007015244 0ustar cwestwheel00000000000000use ExtUtils::MakeMaker; WriteMakefile ( AUTHOR => 'Casey West ', ABSTRACT => "Pid File Manipulation", NAME => 'File::Pid', PREREQ_PM => { 'Class::Accessor::Fast' => '0.19', }, VERSION_FROM => 'lib/File/Pid.pm', ); File-Pid-1.01/MANIFEST0000644000076500000000000000013310170747221014416 0ustar cwestwheel00000000000000Changes lib/File/Pid.pm Makefile.PL MANIFEST This list of files META.yml README t/test.t File-Pid-1.01/META.yml0000644000076500000000000000052410170747334014547 0ustar cwestwheel00000000000000# http://module-build.sourceforge.net/META-spec.html #XXXXXXX This is a prototype!!! It will change in the future!!! XXXXX# name: File-Pid version: 1.01 version_from: lib/File/Pid.pm installdirs: site requires: Class::Accessor::Fast: 0.19 distribution_type: module generated_by: ExtUtils::MakeMaker version 6.24 File-Pid-1.01/README0000644000076500000000000000470110170746733014161 0ustar cwestwheel00000000000000NAME File::Pid - Pid File Manipulation SYNOPSIS use File::Pid; my $pidfile = File::Pid->new({ file => '/some/file.pid', }); $pidfile->write; if ( my $num = $pidfile->running ) { die "Already running: $num\n"; } $pidfile->remove; DESCRIPTION This software manages a pid file for you. It will create a pid file, query the process within to discover if it's still running, and remove the pid file. new my $pidfile = File::Pid->new; my $thisfile = File::Pid->new({ file => '/var/run/daemon.pid', }); my $thisfileandpid = File::Pid->new({ file => '/var/run/daemon.pid', pid => '145', }); This constructor takes two optional paramters. "file" - The name of the pid file to work on. If not specified, a pid file located in "File::Spec->tmpdir()" will be created that matches "(File::Basename::basename($0))[0] . '.pid'". So, for example, if $0 is ~/bin/sig.pl, the pid file will be /tmp/sig.pl.pid. "pid" - The pid to write to a new pidfile. If not specified, $$ is used when the pid file doesn't exist. When the pid file does exist, the pid inside it is used. file my $pidfile = $pidfile->file; Accessor/mutator for the filename used as the pid file. pid my $pid = $pidfile->pid; Accessor/mutator for the pid being saved to the pid file. write my $pid = $pidfile->write; Writes the pid file to disk, inserting the pid inside the file. On success, the pid written is returned. On failure, "undef" is returned. running my $pid = $pidfile->running; die "Service already running: $pid\n" if $pid; Checks to see if the pricess identified in the pid file is still running. If the process is still running, the pid is returned. Otherwise "undef" is returned. remove $pidfile->remove or warn "Couldn't unlink pid file\n"; Removes the pid file from disk. Returns true on success, false on failure. program_name This is a utility method that allows you to determine what "File::Pid" thinks the program name is. Internally this is used when no pid file is specified. SEE ALSO perl. AUTHOR Casey West, . COPYRIGHT Copyright (c) 2005 Casey West. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. File-Pid-1.01/t/0000755000076500000000000000000010170747403013535 5ustar cwestwheel00000000000000File-Pid-1.01/t/test.t0000644000076500000000000000156010170672156014705 0ustar cwestwheel00000000000000use Test::More qw[no_plan]; use strict; $^W = 1; use_ok 'File::Pid'; use File::Spec::Functions qw[tmpdir catfile]; is File::Pid->program_name, 'test.t', 'program_name correct'; my $pid = File::Pid->new; is $pid->pid, $$, 'current process by default'; is $pid->file, catfile(tmpdir,'test.t.pid'), 'pid file correct'; is $pid->write, $$, 'writing file'; is $pid->running, $$, 'we are running'; ok $pid->remove, 'deleted file'; my $child; my $file = catfile(tmpdir, 'test.t.child.pid'); unlink $file; if ( $child = fork ) { #parent waitpid $child, 0; my $cpid = File::Pid->new({file => $file}); is $cpid->pid, $child, "$$:$child: child pid correct"; ok !$cpid->running, 'child is not running'; ok $cpid->remove, 'removed child pid file'; } else { # child File::Pid->new({ file => $file, pid => $$, })->write; # hope for the best }