ps start_time 作为时间戳

ps start_time as timestamp

我目前正在使用 ps 命令来监控一些进程。但是,我希望将时间输出为 Unix 时间戳。

我正在使用的命令:

ps -eo class,s,user,pid,pcpu,stat,start_time,cputime,args --sort=class | \
  awk ' != "RR" &&  != "FF" {print [=12=]; next } 0'

谢谢!

使用 etimes 选项(自进程启动以来经过的时间,以秒为单位)和 awksystime() 函数 returns 当前时间戳(以秒为单位)。

假设第 7 列是 etimes,那么 AWK 表达式应该如下所示:

NR == 1 ? "TIMESTAMP" : systime() - 

示例:

ps -eo class,s,user,pid,pcpu,stat,etimes,cputime,args --sort=class | awk \
  ' != "RR" &&  != "FF" {print [=11=], NR == 1 ? "TIMESTAMP" : systime() -  }'

示例输出

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

如果要替换第七列,只需覆盖</code>变量即可:<code> = systime() - .

如果要进一步分析命令的结果,我会删除 headers 和 --no-headers 选项,例如:

ps --no-headers -eo class,s,user,pid,pcpu,stat,etimes,cputime,args \
  --sort=class | awk ' != "RR" &&  != "FF" {  = systime() - ; print }'

示例输出

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]

请注意,您的脚本中不需要 next 语句,因为它的目的是立即停止处理当前记录并继续处理下一条记录,并且没有语句 (pattern-action 对) 在脚本末尾跳过。