The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Proc::Terminator - Conveniently terminate processes

SYNOPSIS

    use Proc::Terminator;
    
    # Try and kill $pid using various methods, waiting
    # up to 20 seconds
    
    proc_terminate($pid, max_wait => 20);

DESCRIPTION

Proc::Terminator provides a convenient way to kill a process, often useful in utility and startup functions which need to ensure the death of an external process.

This module provides a simple, blocking, and procedural interface to kill a process or multiple processes (not tested), and not return until they are all dead.

Proc::Terminator can know if you do not have permissions to kill a process, if the process is dead, and other interesting tidbits.

It also provides for flexible options in the type of death a process will experience. Whether it be slow or immediate.

This module exports a single function, proc_terminate

proc_terminate($pids, %options)

Will try to terminate $pid, waiting until the process is no longer alive, or until a fatal error happens (such as a permissions issue).

$pid can either be a single PID (a scalar), or a reference to an array of multiple PIDs, in which case they are all attempted to be killed, and the function only returning once all of them are dead (or when no possible kill alternatives remain).

The %options is a hash of options which control the behavior for trying to terminate the pid(s).

max_wait

Specify the time (in seconds) that the function should try to spend killing the provided PIDs. The function is guaranteed to not wait longer than max_wait.

This parameter can also be a fractional value (and is passed to Time::HiRes).

DEFAULT: 10 Seconds.

siglist

An array of signal constants (use POSIX's :signal_h to get them).

The signals are tried in order, until there are no more signals remaining.

Sometimes applications do proper cleanup on exit with a 'proper' signal such as SIGINT.

The default value for this parameter

The default signal list can be found in @Proc::Terminator::DefaultSignalOrder

DEFAULT: [SIGINT, SIGQUIT, SIGTERM, SIGKILL]

grace_period

This specifies a time, in seconds, between the shifting of each signal in the siglist parameter above.

In other words, proc_terminate will wait $grace_period seconds after sending each signal in siglist. Thereafter the signal is removed, and the next signal is attempted.

Currently, if you wish to have controlled signal wait times, you can simply insert a signal more than once into siglist

DEFAULT: 0.75

interval

This is the loop interval. The loop will sleep for ever interval seconds. You probably shouldn't need to modify this

DEFAULT: 0.25

When called in a scalar context, returns true on sucess, and false otherwise.

When called in list context, returns a list of the PIDS NOT killed.

SEE ALSO

signal(7)

kill(2)

Perl's kill

AUTHOR & COPYRIGHT

Copyright (C) 2012 M. Nunberg

You may use and distribute this software under the same terms and conditions as Perl itself.