linux - ps start_time as timestamp -
i'm using ps
command monitor processes. however, like have time output unix timestamp.
the command using:
ps -eo class,s,user,pid,pcpu,stat,start_time,cputime,args --sort=class | \ awk '$1 != "rr" && $1 != "ff" {print $0; next } 0'
thanks!
use etimes
option (elapsed time since process started, in seconds) , awk
's systime()
function returns current timestamp in seconds.
suppose 7th column etimes
, awk expression should this:
nr == 1 ? "timestamp" : systime() - $7
example:
ps -eo class,s,user,pid,pcpu,stat,etimes,cputime,args --sort=class | awk \ '$1 != "rr" && $1 != "ff" {print $0, nr == 1 ? "timestamp" : systime() - $7 }'
sample output
cls s user pid %cpu stat elapsed time command timestamp ts s root 1 0.0 ss 1717 00:00:01 init [3] 1479001705 ts s root 2 0.0 s 1717 00:00:00 [kthreadd] 1479001705 ts s root 3 0.0 s 1717 00:00:00 [ksoftirqd/0] 1479001705
if want replace seventh column, can override $7
variable: $7 = systime() - $7
.
if result of command supposed parsed further, remove headers --no-headers
option, e.g.:
ps --no-headers -eo class,s,user,pid,pcpu,stat,etimes,cputime,args \ --sort=class | awk '$1 != "rr" && $1 != "ff" { $7 = systime() - $7; print }'
sample output
ts s root 1 0.0 ss 1479001705 00:00:01 init [3] ts s root 2 0.0 s 1479001705 00:00:00 [kthreadd] ts s root 3 0.0 s 1479001705 00:00:00 [ksoftirqd/0]
note, there no need next
statement in script, since purpose stop processing current record , go on next record, , there no statements (pattern-action pairs) skip @ end of script.
Comments
Post a Comment