You could use BSD process accounting. lastcomm
doesn't report the elapsed time, but at least on Linux, it's stored in the accounting database and can be queried with the dump-acct
command.
$ sudo accton on # activate process accounting
$ sleep 5.23
$ sudo dump-acct /var/log/account/pacct | grep '^sleep'
sleep |v3| 0.00| 0.00| 523.00| 10031| 10031| 14632.00| 0.00| 15261 416|Mon Jun 2 17:09:37 2014
The elapsed time is the 5th field (above 523 centi-seconds).
The 3rd and 4th fields are the user time and system time, that is the time the CPU (any CPU) spent running that process (any thread of that process).
Note that (at least on Linux), the process is accounted when it (any thread of its) dies and as the process name it had when it died. That means that for instance in:
sh -c 'sleep 4; exec sleep 5'
One process executes sh
and then forks another process to execute sleep 4
and then the first process executes sleep 5
(therefore changing name). In the accounting database, we get:
sleep |v3| 0.00| 0.00| 400.00| 1000| 1000| 10320.00| 0.00| 28867 28866|Tue Jun 3 07:45:52 2014
sleep |v3| 0.00| 0.00| 900.00| 1000| 1000| 10320.00| 0.00| 28866 28801|Tue Jun 3 07:45:52 2014
That is the process that runs sh
and then sleep 5
is accounted as sleep
for a duration of 900 centi-seconds, and there's no mention of sh
.
For firefox
(a heavily multi-threaded application) unfortunately, in my case, I see very short times, because the process is accounted when one short-lived thread of its dies.