time: Tracking execution time

Written by Benjamin Cane on 2011-08-27

Many times in my life as a sysadmin I've needed to time how long a script or process takes to run. This was usually a manual task until I found the awesomeness of the time command.

Example:

[[email protected] ~]$ w  
09:51:24 up 4:41, 2 users, load average: 0.00, 0.01, 0.05  
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT  
bcane tty1 :0 18Aug11 8days 3.40s 0.06s pam: gdm-passwo  
bcane pts/0 :0.0 18Aug11 0.00s 0.03s 0.01s w


[[email protected] ~]$ time w  
09:52:58 up 4:42, 2 users, load average: 0.00, 0.01, 0.05  
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT  
bcane tty1 :0 18Aug11 8days 3.46s 0.06s pam: gdm-passwo  
bcane pts/0 :0.0 18Aug11 0.00s 0.02s 0.00s w  

real 0m0.022s 
user 0m0.002s 
sys 0m0.011s

Or if you want to get fancy you can encapsulate the commands with curly brackets.

[[email protected] ~]$ time { for x in /tmp/v*; do sleep 5; echo $x; done; }  
/tmp/virtual-bcane.Bkon89  
/tmp/virtual-bcane.v9Bm6L  
/tmp/virtual-bcane.WXwFtb  

real 0m15.072s  
user 0m0.001s  
sys 0m0.006s  

Picture of Benjamin Cane

Benjamin's specialty is keeping the lights on for mission critical systems. He is currently building applications that enable high concurrency financial transactions.

Recently Benjamin published his first book; Red Hat Enterprise Linux Troubleshooting Guide. In addition to writing, he has several Open Source projects focused on making Ops easier. These projects include Automatron, a project enabling auto-healing infrastructure for the masses.


Publications

Identify, capture and resolve common issues faced by Red Hat Enterprise Linux administrators using best practices and advanced troubleshooting techniques

What people are saying:
Excellent, excellent resource for practical guidance on how to troubleshoot a wide variety of problems on Red Hat Linux. I particularly enjoyed how the author made sure to provide solid background and practical examples. I have a lot of experience on Red Hat but still came away with some great practical tools to add to my toolkit. - Amazon Review