2

Can someone please help me parse this output to show in total number of minutes?

Here's the command (which works as expected except for the formatting):

ps -eo pid,etime,command | grep some_process | grep -v grep | awk '{print $2}'

Output (in hours, minutes, seconds)

03:01:24

I need the output to look something like:

181.40

(3 hours, 1 minute, 24 seconds displayed as a real number)

Is this possible? Any suggestions are greatly appreciated.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Mike J
  • 1,200
  • 6
  • 20
  • 31

1 Answers1

2
ps -eo pid,etime,command | grep PID | grep -v grep | awk '{print $2}' | awk -F : '{ printf("%.2f\n", $1*60+$2+($3/60)); }'

;)

Edited: An improved version (thx @alexandernst and @Nathan):

ps -eo pid,etimes,command | grep PID | grep -v grep | awk '{printf("%.2f\n", $2/60)}'
Thiago Curvelo
  • 3,711
  • 1
  • 22
  • 38
  • Wow, great. This works as expected. Can I just ask about the logic behind: $1*60+$2+($3/60) Thank you for the quick reply – Mike J Dec 17 '12 at 19:40
  • $1:$2:$3 = hours:minutes:seconds. hours to minutes = $1 * 60. seconds to minutes = $3 / 60. Got it? – Thiago Curvelo Dec 17 '12 at 21:08
  • 3
    This will fail if/when etime doesn't return HH:MM:SS, but just MM:SS (for example, a process that was starting in less than an hour ago) – alexandernst Jun 07 '13 at 13:17
  • 2
    Looks like you can use 'etimes' instead of 'etime' to get seconds directly. – Nathan Lippi Nov 23 '13 at 21:42
  • 1
    The 'etimes' keyword is available on GNU ps but not on BSD ps, at least not on OSX. A more portable solution appears at: [Parse ps etime output into seconds](http://stackoverflow.com/a/14653443/3321356) – markeissler Feb 10 '15 at 04:33
  • No need of all those grep: `ps -eo pid,etimes,command | awk '/PID/ {printf("%.2f\n", $2/60)}'` – nnsense May 03 '20 at 16:31