POE::Component::ProcTerminator - Safely and easily kill runaway processes
This is a POE component wrapping the functionality of Proc::Terminator, but using POE's timers and event loop instead of the built-in (and obviously blocking) sleep-based loop.
Set up the component..
POE::Component::ProcTerminator->spawn(Alias => "proc_terminator");
Later, in your code..
$_[KERNEL]->call(proc_terminator => terminate => $a_pid, { siglist => [ SIGINT, SIGTERM, SIGKILL ] });
And that's all!
Semantics here are identical to that specified in Proc::Terminator. This manpage describes feature specific to the POE component
If the Alias parameter is missing, then it will spawn the default session, called proc_terminator
Alias
proc_terminator
Further options are taken as defaults to be used for subsequent calls to "terminate"
Instruct the component to begin trying to kill $pid, where $pid can either be a single PID, or an array reference of PIDs.
$pid
The second argument may be an optional hashref of options, accepting the same kinds of things that Proc::Terminator's proc_terminate does.
proc_terminate
The more relevant options are
A list of signals which will be used to kill off the process. Signals are tried in succession, until the process is either dead or there are no more signals left in the list. The defaults exist in @Proc::Terminator::DefaultSignalOrder
@Proc::Terminator::DefaultSignalOrder
An interval (in seconds) to wait between shifting the signal list (specified in siglist). In the world of POE, this effectively means how often the timer event will be called.
siglist
Some additional options:
If for whatever reason Proc::Terminator is unable to kill your process, this callback will be invoked with a Proc::Terminator::Batch as its sole argument. See Proc::Terminator for documentation on that object.
Proc::Terminator
Proc::Terminator::Batch
Control the behavior of POE::Component::ProcTerminator when the session terminates prematurely (e.g. the kernel is being shut down, or some other exceptional terminal condition has occured).
POE::Component::ProcTerminator
The default behavior is to not do anything.
This value is one or more of the following flags, OR'd together (these are sub constants, exported to your namespace).
This instructs the cleanup handler to block and loop for this batch, as Proc::Terminator does in the synchronous API. The maximum time each batch can block is 5 seconds (though this might be configurable).
If SIGKILL was not in the original list of signals to send to the process, then push it to the end of the signal-to-send stack. This only makes sense with the PROCTERMf_CLEANUP_BLOCK
SIGKILL
PROCTERMf_CLEANUP_BLOCK
Blindly sends a SIGKILL to the remaining processes. It does not make sense to use this flag with the other flags.
When POE shuts down and the component is about to stop, it will call an iteration of the loop (hopefully killing it). In the future I might want to do something like either invoke a 'real' Proc::Terminate session (with a reasonable time limit) or nuke them all with SIGKILL. Dunno?
Copyright (C) 2012 M. Nunberg
You may use and distribute this software under the same terms and conditions as Perl itself.
To install POE::Component::ProcTerminator::Batch, copy and paste the appropriate command in to your terminal.
cpanm
cpanm POE::Component::ProcTerminator::Batch
CPAN shell
perl -MCPAN -e shell install POE::Component::ProcTerminator::Batch
For more information on module installation, please visit the detailed CPAN module installation guide.