Skip to content
shellhacks.dev
Linux

Get the Elapsed Running Time of a Process with ps

Show how long a process has run with ps -o etime= -p PID, or as plain seconds with etimes. The format is [[dd-]hh:]mm:ss, padded with leading spaces.

1 min read
Summarize in AI

A job looks stuck, and you want to know how long its process has been running.

Shell
sleep 300 &
pid=$!
sleep 2
ps -o etime= -p "$pid"
ps -o etimes= -p "$pid"
ps -o pid,etime,cmd -p "$pid"
kill "$pid"
Text
      00:02
      2
    PID     ELAPSED CMD
    613       00:02 sleep 300

etime prints the time since the process started as [[dd-]hh:]mm:ss, so a process alive for two seconds shows 00:02 and one running for three days shows something like 3-04:05:06. etimes gives the same value in seconds, which is easier to compare in a script. The leading equals sign trick removes the heading. Both columns are in the ps manual.

etimes needs a reasonably recent procps, from the 3.3 series onward, and the macOS and BSD ps have etime but no etimes. The value counts from process start and not from the moment the program began its real work, which differs when a wrapper script starts first.

Share

More snippets

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.