NAME

Proc::tored::PidFile - Manage a service using a pid file

VERSION

version 0.20

SYNOPSIS

  use Proc::tored::PidFile;

  my $pidfile = Proc::tored::PidFile->new(file_path => $pid_file_path);

  if (my $lock = $pidfile->lock) {
    run_service;
  }
  else {
    die "service is already running under process id "
      . $pidfile->running_pid;
  }

DESCRIPTION

Allows the use of a pid file to manage a running service.

METHODS

is_running

Returns true if the pid indicated by the pid file is the current process.

running_pid

Returns true if the pid indicated by the pid file is an active, running process. This is determined by attempting to signal the process using kill(0, $pid).

read_file

Returns the pid stored in the pid file or 0 if the pid file does not exist or is empty.

write_file

Writes the current process id to the pid file. Returns true on success, false if the pid file exists and contains a running process id or if unable to atomically write the pid file out.

clear_file

Truncates the pid file and then unlinks it.

lock

Attempts to write the current process id to the pid file and returns a Guard that will truncate and unlink the pid file if it goes out of scope.

  {
    my $lock = $pidfile->lock;
    run_service;
  }

  # $lock goes out of scope and pid file is truncated and unlinked

AUTHOR

Jeff Ober <sysread@fastmail.fm>

COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Jeff Ober.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.