Skip to content

Instantly share code, notes, and snippets.

@kimmel
Last active Jul 1, 2016
Embed
What would you like to do?
Cleaning up processes with Proc::ProcessTable
use v5.16;
use warnings;
use autodie qw( :all );
use utf8::all;
use POSIX qw( strftime );
use Parallel::ForkManager;
use Proc::ProcessTable;
my $process_count = 12;
my $find = 'sst';
my $duration = 28;
sub run_list {
my $pm = shift;
my $log = shift;
my $entries = shift;
my $counter = 0;
my $p = new Proc::ProcessTable( 'cache_ttys' => 1 );
foreach my $b ( @{$entries} ) {
$counter++;
if ( $counter >= $process_count ) {
foreach my $proc ( @{ $p->table } ) {
if ( ( $proc->{'fname'} eq $find )
and ( time - $proc->{'start'} > $duration ) )
{
say $proc->{'pid'}
. ' running since - '
. scalar strftime( '%Y-%m-%d %H:%M:%S',
localtime( $proc->{'start'} ) );
$proc->kill(9);
}
}
$counter = 0;
}
print {$log} " processing: $b\n";
my $pid = $pm->start and next;
#...
$pm->finish;
}
return;
}
#...
my $pm = Parallel::ForkManager->new($process_count);
run_list( $pm, $log, \@entires );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment