App-ProcUtils
view release on metacpan or search on metacpan
lib/App/ProcUtils.pm view on Meta::CPAN
tags => ['category:filtering'],
},
logic => {
schema => ['str*', in=>['AND','OR']],
default => 'AND',
cmdline_aliases => {
and => {is_flag=>0, summary=>'Shortcut for --logic=AND', code=>sub {$_[0]{logic} = 'AND' }},
or => {is_flag=>0, summary=>'Shortcut for --logic=OR' , code=>sub {$_[0]{logic} = 'OR' }},
},
tags => ['category:filtering'],
},
code => {
schema => 'code*',
description => <<'MARKDOWN',
Code is given <pm:Proc::ProcessTable::Process> object, which is a hashref
containing items like `pid`, `uid`, etc. It should return true to mean that a
process matches.
MARKDOWN
tags => ['category:filtering'],
},
);
our %arg_detail = (
detail => {
summary => 'Return detailed records instead of just PIDs',
schema => 'true',
cmdline_aliases=>{l=>{}},
},
);
our %arg_quiet = (
quiet => {
schema => 'true',
cmdline_aliases=>{q=>{}},
},
);
our @proc_fields = (
# follows the order of 'ps aux'
"uid",
"pid",
"pctcpu",
"pctmem",
"size",
"rss",
"ttydev",
"ttynum",
"state",
"start",
"time",
"cmndline",
"cmajflt",
"cminflt",
"cstime",
"ctime",
"cutime",
"cwd",
"egid",
"euid",
"exec",
"fgid",
"flags",
"fname",
"fuid",
"gid",
"majflt",
"minflt",
"pgrp",
"ppid",
"priority",
"sess",
"sgid",
"stime",
"suid",
"utime",
"wchan",
# not included
# environ
);
sub _proc_obj_to_hash {
my $row = {%{$_[0]}};
$row->{cmdline} = join(" ", grep {$_ ne ''} @{ $row->{cmdline} });
delete $row->{environ};
$row;
}
$SPEC{list_parents} = {
v => 1.1,
summary => 'List all the parents of the current process',
};
sub list_parents {
require Proc::Find::Parents;
[200, "OK", Proc::Find::Parents::get_parent_processes(
$$, {method=>'proctable'})];
}
$SPEC{table} = {
v => 1.1,
summary => 'Run Proc::ProcessTable and display the result',
};
sub table {
require Proc::ProcessTable;
my $t = Proc::ProcessTable->new;
my $resmeta = {};
$resmeta->{'table.fields'} = \@proc_fields;
my @rows;
for my $p (@{ $t->table }) {
my $row = _proc_obj_to_hash($p);
push @rows, $row;
}
[200, "OK", \@rows, $resmeta];
}
sub _kill_or_list {
require Proc::ProcessTable;
require String::Elide::Tiny;
my $which = shift;
my %args = @_;
my $is_or = ($args{logic} // 'AND') eq 'OR' ? 1:0;
my $proc_table = Proc::ProcessTable->new;
my @proc_matches;
ENTRY:
for my $proc_entry (@{ $proc_table->table }) {
( run in 2.343 seconds using v1.01-cache-2.11-cpan-ceb78f64989 )