SYNOPSIS

Aside from the obvious run time vs. compile time factor, this is *exactly* the same as doing

use Unix::PID;
Unix::PID->new()->pid_file('/var/run/this.pid') or die 'The PID in /var/run/this.pid is still running.';

So the "use Unix::PID 'pidfile';" will simplify 99% of the times you'd use $pid->pid_file();

METHODS

Unix::PID->new()

Get a Unix::PID object.

It takes an optional hashref with the following, optional, keys:

'minimum_pid'

The minimum PID that can be kill()ed. If not given or not all digits then it defaults to 11.

'ps_path'

The path to the 'ps' binary you want to use. The value gets passed to the object's set_ps_path().

'use_open3'

By default this is true. When given a false value it will not load IPC::Open3 and it will use backtick execution of system commands.

This will gain you the memory of IPC::Open3 but lose it's many benefits. One such benefit is the capturing of the command's STDERR for reporting via get_errstr().

$pid->set_ps_path()

Set the path where ps is at. If not set via this method or in new() or previously then _raw_ps() looks for it in several common places and sets it to that if it finds it. returns true if what you specify is ok and false otherwise.

$pid->set_ps_path('/usr/util/bin');

$pid->get_ps_path()

Get the path that the object thinks ps is at

print 'I am using ps at ' . $pid->get_ps_path();

$pid->get_pidof()

Gets the pid(s) matching the given command. In scalar context returns the first, in array returns all.

The first arg is a string to match against running PIDs, the second, option arg, if true makes it match exactly as given.

$pid->is_command_running()

$pid->pid_file()

Takes three arguments, the first is the pid file, the second, optional, argument is the pid to write to the file (defaults to $$), the third, also optional, argument is "retry" configuration described below.

If the pid file exists it checks to see if the pid in it is running and if so it returns undef, if not it writes the second argument (or $$) to the file and returns 1.

It returns 0 if the pid file read or write open() fails. (IE you could use $! in your "or whatever")

# make sure this only runs one at a time
Unix::PID->new()->pid_file('/var/run/this.pid') or die 'This is already running';

Upon success it also sets up and END block to remove the file if the PID we setup was our PID.

The "retry" configuration mentioned above is a reference to an array. The first item is the number of times to "retry" processing of an existing pid file. The additonal arguments are what to do after each pass (except the last pass which returns false afterward). The index corresponds to the pass number. e.g. $ar->[1] is what to do after the first pass, $ar->[2] is what to do after the second pass, and so on.

The value can be a number, in which case it sleep()s that many seconds, or a code ref. The code ref is passed the Unix::PID object as thre first argument, pid file in question as the second argument and the number of passes thus far as the third.

The default "retry" configuration is [3,1,2].

$pid->pid_file_no_unlink()

Just like $pid->pid_file() except no END block cleanup is setup. Useful for doing pid files for a sporking daemon.

$pid->get_pid_from_pidfile()

Takes one argument, the pid file whose PID you want.

Returns the pid stored in the given pid file, 0 if the pid file does not exist or the contents are not numeric. return;s on failure to open the existing pid file.

$pid->kill_pid_file()

Takes one argument, the pid file whose PID you want kill()ed. It unlinks the pid file after its successful run.

It returns true (if the file exists, the pid. otherwise 1) if all is well, 0 if it exists but could not be opened, undef if the pid could not be killed, and -1 if it could not be cleaned up after it was successfully killed.

$pid->kill_pid_file_no_unlink();

Just like $pid->kill_pid_file() but the pid file is not unlink()ed. (and it likewise does not return -1)

$pid->kill()

Takes one mandatory argumentL the PID to kill, and one option argument: the seconds to wait for kill() to finish before we check for the PID.

If its running it first tries kill 1 and if that fails it tries kill 9.

Returns undef if the PID was running and could not be killed, true if its not running or was killed successfully.

$pid->kill( $mypid ) or warn "Could not kill PID $mypid: $!";

$pid->non_blocking_wait()

Does a non-blocking wait for all pending zombie processes

$pid->wait_for_pidsof()

This function waits for processes matching your criteria to finish before going on.

Its single argument is a hash ref whose keys are the following:

pid_list

An array ref of numeric PIDs to wait on. If this exists and is an array ref it will be used instead of get_pidof

get_pidof

The value is the same as you'd pass to $pid->get_pidof, defaults to $pid->get_command($$) to wait for process that have the exact same command to stop.

sleep_for

Number of seconds to sleep between checking on the pids. defaults to 60

If an array ref is passed the sleep time cycles through this list.

If a hashref is sent, and it has a key of the value 'fibonacci' each cycle uses the next fibonnaci number as the time to sleep, starting with the first.

If both use_hires_nanosleep and use_hires_usleep are true use_hires_nanosleep is used.

max_loops

number of times to check before giving up, defaults to 5

pre_sleep

An optional code reference to do before it sleeps, this would be useful to let everyone know whats going on.

@_ contains the number of the loop you're in and an array ref of the pids you're currently waiting on

hit_max_loops

A code reference to do once you've looped 'max_loops' times. By default it die()'s. I purposefully die()ed instead of croak to encourage you to specify it so you can handle it properly according to your needs.

@_ contains the number of the loop you're in and an array ref of the pids you're currently waiting on

For clarity and maintainability I highly recommend specifying each option so you (and the poor souls who have to maintain your code later) will have some sort of idea what you were trying for instead having to guess and hack away deeper into code,