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 seconds?
egreped
to one only line so no need for a loop.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 1If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With