I'm trying to write a script that can monitor a process's CPU usage over an interval (to create a graph).

So far, this is the command I'm using

ps -p $PROCID -o cputime,etimes

My only concern is that the output of cputime appears to be [dd]hh:mm (or something similar, can't remember off the top of my head now)

Is there a way to format cputime in seconds, kind of like etime -> etimes to get elapsed time in seconds?

Edit: This is the output that I'm currently receiving

2-03:01:33 2653793

I'd like the first parameter to be formatted in seconds, not days-hours:minutes:seconds.

  • can you please add the output you receive now and the output you'd like to receive?
    – Simply_Me
    Sep 20, 2014 at 21:11
  • @Simply_Me I have edited the post Sep 20, 2014 at 21:13
  • @John1024 Yes, it is, that's the amount of days. Sep 20, 2014 at 21:44

3 Answers 3


This converts the first time to seconds:

ps -p $PROCID -o cputime,etimes | awk -F'[: ]+' '/:/ {t=$3+60*($2+60*$1); print t,$NF}'

As an example, the ps command produces:

$ ps -p 5403 -o cputime,etimes
01:33:38 1128931

The awk command processes that and returns:

ps -p 5403 -o cputime,etimes | awk -F'[: ]+' '/:/ {t=$3+60*($2+60*$1); print t,$NF}'
5618 1128931


  • -F'[: ]+'

    This tells awk to treat both colons and spaces as field separators. This way, the hours, minutes, and seconds appear as separate fields.

  • /:/ {t=$3+60*($2+60*$1); print t,$NF}

    The initial /:/ restricts the code to working only on lines that include a colon. This removes the header lines. The number of seconds is calculated from hours, minutes, seconds via t=$3+60*($2+60*$1). The resulting value for t is then printed along side with the elapsed time.

Handling days

If ps produces days,hours,minutes,seconds, as in:


Then, use this code instead:

ps -p $PROCID -o cputime,etimes | awk -F'[-: ]+' '/:/ {t=$4+60*($3+60*($2+24*$1)); print t,$NF}'

If days may or may not be prepended to the output, then use this combination command:

ps -p $PROCID -o cputime,etimes | awk -F'[-: ]+' '/:/ && NF==5 { t=$4+60*($3+60*($2+24*$1)); print t,$NF} /:/ && NF==4 {t=$3+60*($2+60*$1); print t,$NF}'
  • Forgive me if I'm mistaken, but it doesn't look like this command is taking into account days, which can also be prepended Sep 20, 2014 at 21:42
  • That is correct: my system did not show days for any process. I can guess what days might look like, but can you provide an actual example?
    – John1024
    Sep 20, 2014 at 21:45
  • 2-03:01:33 is 2 days, 3 hours, 1 minute, and 33 seconds. Days doesn't always get prepended though. On my system it's only prepended if the process has been alive for more than 24 hours. Thanks for your help so far btw, I appreciate it Sep 20, 2014 at 21:45
  • @user1772510 Answer updated to handle days.
    – John1024
    Sep 20, 2014 at 21:49
  • Thanks for all of your help, if I could upvote I'd upvote as well (not enough rep in this community) I appreciate it deeply! Sep 20, 2014 at 22:01

If you don't want to bring awk in the game, a pure bash solution (t_str contains the formated string, t_sec the decoded time in seconds):

# Decode the CPU time format [dd-]hh:mm:ss.
IFS="-:" read c1 c2 c3 c4 <<< "$t_str"
if [ -n "$c4" ]

You don't specify which operating system this is for; if it is Linux, and given that you're interested in monitoring a specific process, you might find it more worthwhile to parse /proc/$PROCID/stat - see the proc(5) manual page for details.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .