=head1 NAME

Unix::PID - Perl extension for getting PID info.

=head1 SYNOPSIS

   use Unix::PID;
   my $pid = Unix::PID->new();

or specify where ps is at:

   my $pid = Unix::PID->new({ 'ps_path' => '/usr/util/bin' });

or simplify pid file use by:

   use Unix::PID '/var/run/this.pid';

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();

=head1 METHODS

=head2 Unix::PID->new()

Get a Unix::PID object. 

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

=over 4

=item 'minimum_pid'

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

=item 'ps_path'

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

=item 'use_open3'

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

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

=back

=head2 $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');

=head2 $pid->get_ps_path()

Get the path that the object thinks ps is at
   
    print 'I am using ps at ' . $pid->get_ps_path();

=head2 $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->get_pidof("waldo --foo");

returns pids whose commands contains "waldo --foo" (IE would match 'bin/waldo --foo')

    $pids->get_pidof('waldo --foo', 1);

returns pids whose commands are *entirely* 'waldo --foo' (IE would not match 'bin/waldo --foo')

So for instance to see all processes that are exactly the same as the current:

   $pids->get_pidof( $pids->get_command($$), 1 );

To see in script.pl how many others of itself are running under force:

   $pids->get_pidof('script.pl --force');

The current script's PID (IE $$) is never included in this output.

=head2 $pid->pid_info( $pid )

Get an array (or array ref in scalar context ) of $pid's USER, PID, %CPU, %MEM, VSZ, RSS, TT, STAT, STARTED, TIME, COMMAND

This may vary on your system so check the header of 'ps u -p NUMERIC_PID_HERE' on your system.

=head2 $pid->pid_info_hash()

Same info as pid_info except you get a hash (or hashref in scalar context) with the keys: USER, PID, %CPU, %MEM, VSZ, RSS, TT, STAT, STARTED, TIME, COMMAND and values that correspond to each one.

This may vary on your system so check the header of 'ps u -p NUMERIC_PID_HERE' on your system.

=head2 $pid->is_*running

Check if a pid or command is running or not, returns 1 or 0

=head3 $pid->is_running()

If the first argument is all digits then this it calls $pid->is_pid_running for you. Otherwise it calls $pid->is_command_running() for you.

=head3 $pid->is_pid_running()

    if($pid->is_pid_running($miscpid)) {
        warn "PID $miscpid is running, you had better go catch it";
    } 

=head3 $pid->is_pidfile_running()

Takes one argument, the pid file whose PID you're interested in.

Returns the numeric pid stored in the given pid file if it is running, otherwise it return;s

=head3 $pid->is_command_running()

    if($pid->is_comand_running($cmd)) {
        warn "$cmd is still going strong";
    }

If the second argument is true it acts just like get_pidof()

=head2 $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].

=head2 $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.

=head2 $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.

=head2 $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.

=head2 $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)

=head2 $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: $!";

=head2 $pid->non_blocking_wait()

Does a non-blocking wait for all pending zombie processes

=head2 $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:

=over

=item 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

=item 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.

=item 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.

See L<Math::Fibonacci::Phi>

=item use_hires_usleep

If true Time::HiRes::usleep() is used instead of sleep() so that you can have it sleep (via "sleep_for") for fractions of seconds in microseconds.

See L<Time::HiRes>

=item use_hires_nanosleep

If true Time::HiRes::nanosleep() is used instead of sleep() so that you can have it sleep (via "sleep_for") for fractions of seconds in nanoseconds.

See L<Time::HiRes>

If both use_hires_nanosleep and use_hires_usleep are true use_hires_nanosleep is used.

=item max_loops

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

=item 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

=item 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

=back

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, 

Here is a complete example:

    # do some initialization stuff

    $pid->wait_for_pidsof(
        {
            'get_pidof'     => 'deepthought --get-ultimate-answer',
            'sleep_for'     => 31_556_926, # check once a year
            'max_loops'     => 7_500_000,  # for 7.5 million years
            'pre_sleep'     => sub {
                my($we_are_in_loop_number, $waiting_on_these_pids_ref) = @_;
                print "Currently in year $we_are_in_loop_number waiting for deepthought: " 
                      . join ',', @{ $waiting_on_these_pids_ref };
            },
            'hit_max_loops' => sub {
                my($we_are_in_loop_number, $waiting_on_these_pids_ref) = @_;
                croak "Sorry mice, even after $we_are_in_loop_number years deepthought is still just watching TV: "
                      . join ',', @{ $waiting_on_these_pids_ref };
            },
        }
    );

    # continue on now that the answer has been had :)

Obviously its not efficient to have it sleep for a year between checks 7.5 million times just to calculate 42, its just an example :)

=head2 $pid->get_errstr();

Any errors from _raw_ps can be fetched from $pid->get_errstr()

The "or die $pid->get_errstr()" paradigm doesn't work because any undef or otherwise "false" values do not necessarily indicate an error.

So you can do:

   die $pid->get_errstr() if $pid->get_errstr();

=head2 $pid->get_*

There are get_ functions for each \w+ that can be passed to ps's -o option, 

Each one takes a pid as the only argument.

    my $i_am_command = $pid->get_command($$);

=head2 $pid->_raw_ps(@ps_args);

Calls ps with the given args and returns an array of each line (or the first line in scalar context).

=head1 TO DO

I'd be happy to add additional functionality if it belongs here, just drop me a line :)

=head1 AUTHOR

Daniel Muey, L<http://drmuey.com/cpan_contact.pl>

=head1 COPYRIGHT AND LICENSE

Copyright 2005 by Daniel Muey

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

=cut
