Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like to avoid doing this by launching the process from a monitoring app.

share|improve this question
add comment

5 Answers

up vote 35 down vote accepted

ps -p $$ -o etime=

Where $$ is the PID of the process you want to check. This will return the elapsed time in the format [[dd-]hh:]mm:ss.

The ps program gets this from /proc/$$/stat, where one of the fields (see man proc) is process start time. This is, unfortunately, specified to be the time in jiffies (an arbitrary time counter used in the Linux kernel) since the system boot. So you have to determine the time at which the system booted (from /proc/stat), the number of jiffies per second on this system (which turns out to be hard to determine, although some safe guesses can be made as long as you're not worried about keeping it working in the future), and then do the math to get the elapsed time in a useful format.

So it's convenient that ps does that all for you. :)

share|improve this answer
Hi! Is etime= a typo? I can only find etime in the man pages. –  Kent Pawar Jun 27 '13 at 13:01
@KentPawar It's not a typo. The empty = suppresses the header. Try it without, or try ps -p $$ -o etime="Silly Header Here" –  mattdm Jun 27 '13 at 13:28
ps -p $(pgrep find) -o etime= –  mafro Sep 11 '13 at 23:07
add comment


% ps -o stime,time $$
Jan30 00:00:06

i.e. that shell was started on January 30 and totaled about 6 seconds of CPU time.

There may be more precise or more parseable but less portable ways to get this information. Check the documentation of your ps command or your proc filesystem.

Under Linux, this information lives in /proc/$pid/stat.

awk '{print "CPU time: " $14+$15; print "start time: " $22}' /proc/$$/stat

The CPU time is in jiffies; I don't know offhand how to find the jiffy value from the shell. The start time is relative to the boot time (found in /proc/uptime).

share|improve this answer
Finding the value of HZ (that is, jiffies per second) turns out to be ridiculously complicated! From comments in the sysinfo.c in the procps package, one can a) include the kernel header file (and recompile if a different kernel is used, b) use the posix sysconf() function, which, sadly, uses a hard-coded value compiled into the c library, or c) ask the kernel, and there's no official interface to doing that. So, the code includes a series of kludges by which it determines the correct value. Wow. –  mattdm Feb 22 '11 at 21:54
add comment

ps takes a -o option to specify the output format, and one of the available columns is etime. According to the man page:

etime - elapsed time since the process was started, in the form [[dd-]hh:]mm:ss.

Thus you can run this to get the PID and elapsed time of every process:

$ ps -eo pid,etime

If you want the elapsed time of a particular PID (e.g. 12345), you can do something like:

$ ps -eo pid,etime | awk '/^12345/ {print $2}'

(Edit: Turns out there's a shorter syntax for the above command; see mattdm's answer)

share|improve this answer
add comment
ps -eo pid,comm,cmd,start,etime | grep -i X

X is the name of the process

share|improve this answer
add comment

If you can run time and then execute a command you will get exactly what you are looking for. You cannot do this against an already-running command.

[0] % time sleep 20

sleep 20 0.00s user 0.00s system 0% cpu 20.014 total

share|improve this answer
Do you know how can I do it on a running process monitoring until it ends? –  lrkwz Nov 21 '12 at 22:56
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.