Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the right way to kill child processes in perl before exiting?
    primarykey
    data
    text
    <p>I'm running an IRC Bot (<a href="http://search.cpan.org/perldoc/Bot::BasicBot#forkit" rel="noreferrer">Bot::BasicBot</a>) which has two child processes running <a href="http://search.cpan.org/perldoc/File::Tail" rel="noreferrer">File::Tail</a> but when exiting, they don't terminate. So I'm killling them using <a href="http://search.cpan.org/perldoc/Proc::ProcessTable" rel="noreferrer">Proc::ProcessTable</a> like this before exiting:</p> <pre><code>my $parent=$$; my $proc_table=Proc::ProcessTable-&gt;new(); for my $proc (@{$proc_table-&gt;table()}) { kill(15, $proc-&gt;pid) if ($proc-&gt;ppid == $parent); } </code></pre> <p>It works but I get this warning:</p> <pre> 14045: !!! Child process PID:14047 reaped: 14045: !!! Child process PID:14048 reaped: 14045: !!! Your program may not be using sig_child() to reap processes. 14045: !!! In extreme cases, your program can force a system reboot 14045: !!! if this resource leakage is not corrected. </pre> <p>What else can I do to kill child processes? The forked process is created using the <a href="http://search.cpan.org/perldoc/Bot::BasicBot#forkit" rel="noreferrer">forkit</a> method in <a href="http://search.cpan.org/perldoc/Bot::BasicBot" rel="noreferrer">Bot::BasicBot</a>.</p> <p>Sample script:</p> <pre><code>package main; my $bot = SOMEBOT-&gt;new ( server =&gt; 'irc.dal.net', channels =&gt; ['#anemptychannel'] ); $SIG{'INT'} = 'Handler'; $SIG{'TERM'} = 'Handler'; sub Handler { $bot-&gt;_stop('Leaving.'); } $bot-&gt;run; package SOMEBOT; use base qw(Bot::BasicBot); use File::Tail; use Proc::ProcessTable; sub irc_error_state { die if $_[10] =~ /Leaving\./; } sub help { return; } sub stop_state { my $parent=$$; my $proc_table=Proc::ProcessTable-&gt;new(); for my $proc (@{$proc_table-&gt;table()}) { kill(15, $proc-&gt;pid) if ($proc-&gt;ppid == $parent); } die; } sub connected { my $self = shift; $self-&gt;forkit ( run =&gt; \&amp;announcer, body =&gt; '/home/somebody/somefile.txt', channel =&gt; '#anemptychannel', ) unless $self-&gt;{log1}; $self-&gt;{log1} = 1; $self-&gt;forkit ( run =&gt; \&amp;announcer, body =&gt; '/home/somebody/anotherfile.txt', channel =&gt; '#anemptychannel', ) unless $self-&gt;{log2}; $self-&gt;{log2} = 1; } sub announcer { my $announcefile = shift; my $file=File::Tail-&gt;new(name =&gt; $announcefile, maxinterval=&gt;5, adjustafter=&gt;7); while (defined(my $line=$file-&gt;read)) { chomp $line; print "$line\n"; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload