These are possible output formats for ps h -eo etime
21-18:26:30
15:28:37
48:14
00:01
How to parse them into se
Yet another bash solution, which works any number of fields:
ps -p $pid -oetime= | tr '-' ':' | awk -F: '{ total=0; m=1; } { for (i=0; i < NF; i++) {total += $(NF-i)*m; m *= i >= 2 ? 24 : 60 }} {print total}'
Explanation:
-
to :
so that string becomes 1:2:3:4
instead of
'1-2:3:4', set total to 0 and multiplier to 1Here's mine Perl one liner:
ps -eo pid,comm,etime | perl -ane '@t=reverse split(/[:-]/,$F[2]); $s=$t[0]+$t[1]*60+$t[2]*3600+$t[3]*86400; print "$F[0]\t$F[1]\t$F[2]\t$s\n"'
Undefined values are rendering to zero, so they won't have effect on the sum of seconds.