sábado, 16 de enero de 2010

Trying to measure the total time used by a process in GNU/Linux...

Are you trying to measure how long (in CPU, and in total) does it take a process you are running in Linux? Yeah, you do that from time to time to see the performance of your machine, and you always execute the time command. Something like:

[host]$ time somecomnand
...

real 0m1.127s
user 0m0.002s
sys 0m0.015s

great! That is what we wanted... except that we would probably like to run this automatically and get the output to some log file. So you try to redirect the output and it doesn't work. Therefore, you run man time to see if you find something there and you see the -o outputfile parameter. Yeah! That's what you need, so you try:

[host]$ time -o time.log somecommand
-bash: -o: command not found

real 0m0.002s
user 0m0.000s
sys 0m0.002s

Ups! What happened?

Well, the problem here is that you are really using time reserved word in the bash shell. And the solution is to run the time program in your linux distribution by specifying the full path to it:

[host]$ /usr/bin/time -o time.log yes
...
[host]$ cat time.log
4288.24user 80.40system 1:53:44elapsed 64%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (277major+178073minor)pagefaults 0swaps

The format is slightly different, but you have everything you need.

No hay comentarios:

Publicar un comentario