Proc::PidUtil - PID file management utilities


Proc-PidUtil documentation Contained in the Proc-PidUtil distribution.

Index


Code Index:

NAME

Top

Proc::PidUtil - PID file management utilities

SYNOPSIS

Top

  use Proc::PidUtil qw(
	if_run_exit
	is_running
	make_pidfile
	zap_pidfile
	get_script_name
	:all
  );

DESCRIPTION

Top

Proc::PidUtil provides utilities to manage PID files

* $rv = if_run_exit('path',$message);

This routine checks for a file named:

  '(scriptname).pid

in the the $path directory. If a file is found and the PID found in the file is currently a running job, there is no return, the subroutine prints the (scriptname): $pid, $message to STDERR and exits.

If there is no file or the PID does not match a running job, run_exit returns true.

  input:	path for pidfiles
  return:	true if not running
		else exits

Note: also exits if $path is false

* $rv = is_running('path2pidfile');

Check that the job described by the pid file is running.

  input:	path 2 pid file
  returns:	pid or false (0) if not running

* $rv = make_pidfile('path2pidfile',$pid);

Open a pid file and insert the pid value.

  input:	path 2 pid file,
		pid value || $$
  returns:	pid or false (0) on error

* $rv = zap_pidfile($path);
  input:	path for pidfiles
  returns:	return value of 'unlink'

* $me = get_script_name();

This function returns the script name portion of the path found in $0;

  input:	none
  returns:	script name

  i.e.  if the script name is:
  /usr/local/stuff/scripts/sc_admin.pl

  $me = get_script_name();

  returns ('sc_admin.pl')

DEPENDENCIES

Top

	none

EXPORT_OK

Top

	if_run_exit
        is_running
        make_pidfile
	zap_pidfile
        get_script_name

EXPORT_TAGS

Top

	:all

COPYRIGHT

Top

AUTHOR

Top

Michael Robinton <michael@bizsystems.com>


Proc-PidUtil documentation Contained in the Proc-PidUtil distribution.
#!/usr/bin/perl
package Proc::PidUtil;

use strict;
#use diagnostics;
use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);

require Exporter;
@ISA = qw(Exporter);

$VERSION = do { my @r = (q$Revision: 0.08 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };

%EXPORT_TAGS = (
  all	=> [qw(
	is_running
	make_pidfile
	zap_pidfile
	get_script_name
	if_run_exit
  )],
);

Exporter::export_ok_tags('all');

sub if_run_exit {
  my($path,$message) = @_;
  my $me = get_script_name();
  unless ($path) {
    print STDERR "$me: $path not found\n";
    exit;
  }
  unless (-w $path) {
    print STDERR "$me: $path not writable, check permissions\n";
    exit;
  }
  my $pidfile = $path .'/'. $me . '.pid';

  my $job = is_running($pidfile);
  if ($job) {
    print STDERR "$me: $job, $message\n"
	if $message;
    exit;   
  }
  make_pidfile($pidfile);
}

sub is_running {
  my($pidfile) = @_;
  local *PID;
  return 0 unless 
	-e $pidfile && 
	-r $pidfile &&
	open(PID,$pidfile);
  local $_ = <PID> || 0;		# get pid
  close PID;
  chomp;
  /^\d+$/;
  $_  = $&;
  return 0 unless $_ && $_ !~ /\D/;	# skip bogus pid files
  return (kill 0, $_) ? $_ : 0;
}

sub make_pidfile {
  my($pidfile,$pid) = @_;
  $pid = $$ unless $pid;
  local *PID;
  return 0 unless open(PID,'>'.$pidfile);
  print PID $pid,"\n";
  close PID;
  return $pid;
}

sub zap_pidfile {
  my ($path) = @_;
  my $me = get_script_name();
  my $pidfile = $path .'/'. $me . '.pid';
  unlink $pidfile;
}

sub get_script_name {
  $0 =~ m|[^/]+$|;
  return $&;
}

1;