2

I want to know how long my programs have been used. For example if I use my web browser I want to know the start and the close time to obtain a total of use time.

Basically I want to know when an application starts an when its ends, and in function of the event do something like register the time in a file.

I've searched about listening kernel events or something like that but nothing. Also i tried with supervisord but it says that the process closes too quickly and did not record anything.

Another of my attempts was to use the pyinotify library to monitor the /proc folders but that was a complete failure.

I'm working in Ubuntu. Any tips?

These are my sources

3
3

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.

1
  • That was what I looking for. What is the difference between user time, system time and effective time. Thanks!!!!
    – Inkognito
    Jun 3 '14 at 1:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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