I would like to avoid doing this by launching the process from a monitoring app.
On Linux with the
Or, with newer versions of the procps-ng tool suite (3.3.0 or above) on Linux or on FreeBSD 9.0 or above (and possibly others), use:
(with an added
On Linux, the
It turns out to be ridiculously complicated to find the value of HZ (that is, jiffies per second). From comments in
So it's convenient that
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
Under Linux, this information lives in
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
Thus you can run this to get the PID and elapsed time of every process:
If you want the elapsed time of a particular PID (e.g. 12345), you can do something like:
(Edit: Turns out there's a shorter syntax for the above command; see mattdm's answer)
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.
 % time sleep 20
sleep 20 0.00s user 0.00s system 0% cpu 20.014 total