Stat por processo pode ser encontrado em
/proc/$PID/stat
Você pode ler a documentação executando man proc
, pesquisar por /proc/[pid]/stat
:
man -P'less +/\\/proc\\/\\[pid\\]\\/stat' proc
...
(14) utime %lu Amount of time that this process has been scheduled in user mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK)). This includes guest time, guest_time (time spent running a virtual CPU, see below), so that applications that are not aware of the guest time field do not lose that time from their calculations.
(15) stime %lu Amount of time that this process has been scheduled in kernel mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK)).
Nota: Os outros campos podem ser importantes, por exemplo, se sua ferramenta gerar muitos subprocessos (filhos).
Por exemplo, há um perl rápido fazendo esse trabalho, mas com um atraso fixo de 1 segundo:
#!/usr/bin/perl -w
my ( $lut, $lst ) = ( 0, 0 );
my $pid = $ARGV[0] or die "No pid";
open FH, "</proc/$pid/stat" or die "Can't open \'\'/proc/$pid/stat''";
open UT, "</proc/uptime" or die "Can't open \'\'/proc/uptime''";
sub getvals {
seek FH, 0, 0;
seek UT, 0, 0;
my $st = <FH>;
my $ut = <UT>;
my $out = $lut;
my $ost = $lst;
$lut = ( split " ", $ut )[0] * 100;
$lst = eval( join( "+", ( split " ", $st )[ 13, 14 ] ) );
return $lut - $out, $lst - $ost;
}
getvals;
$| = 1;
while (1) {
sleep 1;
printf "%7.2f%%\n", eval sprintf "100/%s*%s", getvals;
};