Conveniently terminate processes
use Proc::Terminator; # Try and kill $pid using various methods, waiting # up to 20 seconds proc_terminate($pid, max_wait => 20);
\*(C`Proc::Terminator\*(C' 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.
\*(C`Proc::Terminator\*(C' 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, \*(C`proc_terminate\*(C' 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 \s-1PID\s0 (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). 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 \*(C`max_wait\*(C'. This parameter can also be a fractional value (and is passed to Time::HiRes). \s-1DEFAULT\s0: 10 Seconds. An array of signal constants (use \s-1POSIX\s0's \*(C`:signal_h\*(C' 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 \*(C`SIGINT\*(C'. The default value for this parameter The default signal list can be found in @Proc::Terminator::DefaultSignalOrder \s-1DEFAULT\s0: \*(C`[SIGINT, SIGQUIT, SIGTERM, SIGKILL]\*(C' This specifies a time, in seconds, between the shifting of each signal in the \*(C`siglist\*(C' parameter above. In other words, \*(C`proc_terminate\*(C' will wait $grace_period seconds after sending each signal in \*(C`siglist\*(C'. 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 \*(C`siglist\*(C' \s-1DEFAULT\s0: 0.75 This is the loop interval. The loop will sleep for ever \*(C`interval\*(C' seconds. You probably shouldn't need to modify this \s-1DEFAULT\s0: 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 \s-1PIDS\s0 \s-1NOT\s0 killed.
This exists mainly to provide compatibility for event loops. While \*(C`proc_terminate\*(C' loops internally, event loops will generally have timer functions which will call within a given interval.
In the \s-1OO\s0 interface, one instantiates a \*(C`Proc::Terminator::Batch\*(C' object which contains information about the PIDs the user wishes to kill, as well as the signal list (in fact, \*(C`proc_terminate\*(C' is a wrapper around this interface)
Proc::Terminator::Batch methods
Proc::Terminator::Batch->with_pids($pids,$options)
Creates a new \*(C`Proc::Terminator::Batch\*(C'. The arguments are exactly the same as that for proc_terminate.
Since this module does not actually loop or sleep on anything, it is important to ensure that the \*(C`grace_period\*(C' and \*(C`max_wait\*(C' options are set appropriately.
In a traditional scenario, a timer would be associated with this object which would fire every \*(C`grace_period\*(C' seconds.
$batch->loop_once()
Iterates once over all remaining processes which have not yet been killed, and try to kill them.
Returns a true value if processes still remain which may be killed, and a false value if there is nothing else to do for this batch.
More specifically, if all processes have been killed successfully, this function returns 0. If there are still processes which are alive (but cannot be killed due to the signal stack being empty, or another error), then \*(C`undef\*(C' is returned.
$batch->badprocs
Returns a reference to an array of \*(C`Proc::Terminator::Ctx\*(C' objects which were not successfully terminated. The Ctx object is a simple container. Its \s-1API\s0 fields are as follows:
The numeric \s-1PID\s0 of the process
A reference to an array of remaining signals which would have been sent to this process
This is the captured value of $! at the time the error occured (if any). If this is empty, then most likely the process did not respond to any signals in the signal list.
signal\|(7)
kill\|(2)
Perl's kill
Copyright (C) 2012 M. Nunberg
You may use and distribute this software under the same terms and conditions as Perl itself.