I would like to avoid doing this by launching the process from a monitoring app.
|
|
|
On Linux with the
Where Using 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 As user @336_ notes, on Linux (this is not portable), you can use the
will give you the time that process |
|||||||||||||||||||||
|
|
Portable:
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) |
||||
|
|
X is the name of the process |
|||||
|
|
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 |
|||||
|
|
Unsure why this has not yet been suggested: on Linux you can This behavior is explicitly designed to return the process start time, which it can do at high resolution, and which the kernel can do accurately without the jiffies hacks since the kernel can (obviously) simply check the relevant information. The access, data-modification and status change fields all return the process start time. Best of all, you can use NOTE that this does not work with |
|||||||||
|