Вы находитесь на странице: 1из 8

2/8/2014

Linux Time Command Examples

Home
Free eBook
Start Here
Contact
About

Linux Time Command Examples


by Himanshu Arora on January 26, 2012
12

Like

Tw eet

16

There are times when you might want to profile your program on parameters like:
Time taken by program in user mode
Time taken by program in kernel mode
Average memory usage by the program
etc
On Linux we have a utility time that is designed specifically for this purpose. The utility time takes a program name as
an input and displays information about the resources used by the program. Also, if the command exists with non-zero
status, this utility displays a warning message and exit status.
The syntax of time is :
/usr/bin/time [options] program [arguments]

In the above syntax, options refer to set of optional flag/values that can be passed to time utility to set or unset a
particular functionality. The following are the available time command options:
-v, verbose : This option is passed when a detailed description of the output is required.
quite : This option prevents the time utility to report the status of the program.
-f, format : This option lets the user to control the format of output of the time utility.
-p, portability : This option sets the following output format to make the output in conformance with POSIX
real %e
http://www.thegeekstuff.com/2012/01/time-command-examples/

1/8

2/8/2014

Linux Time Command Examples

user %U
sys %S

-o FILE, output=FILE : This option lets the user to redirect the output of time utility to a file. This option lets
the time utility to overwrite the file FILE.
-a, append : This option lets the time utility to append the information to file FILE rather than overwriting it.
When the time command is run, following is the kind of output it gives :
# /usr/bin/time ls
anaconda-ks.cfg bin install.log install.log.syslog mbox
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 3888maxresident)k
0inputs+0outputs (0major+304minor)pagefaults 0swaps

As we can see above, apart from executing the command, the last two lines of the output are the resource information
that time command outputs.
Note: In the above example, the command time was run without any options. So this is a default output generated by
the time command, which is not formatted properly.
As we can see from the output, the default format of the output generated is :

%Uuser %Ssystem %Eelapsed %PCPU (%Xtext+%Ddata %Mmax)k


%Iinputs+%Ooutputs (%Fmajor+%Rminor)pagefaults %Wswaps

The Format Option


This option lets the user to decide the output generated by time command. In the last section we discussed the default
format that is used in output. Here in this section, we will learn how to specify customized formats.
The format string usually consists of `resource specifiers interspersed with plain text. A percent sign (`%) in the format
string causes the following character to be interpreted as a resource specifier.
A backslash (`\) introduces a `backslash escape, which is translated into a single printing character upon output. `\t
outputs a tab character, `\n outputs a newline, and `\\ outputs a backslash. A backslash followed by any other
character outputs a question mark (`?) followed by a backslash, to indicate that an invalid backslash escape was given.
Other text in the format string is copied verbatim to the output. time always prints a newline after printing the resource
use information, so normally format strings do not end with a newline character (or `0).
http://www.thegeekstuff.com/2012/01/time-command-examples/

2/8

2/8/2014

Linux Time Command Examples

For example :
$ /usr/bin/time -f "\t%U user,\t%S system,\t%x status" date
Sun Jan 22 17:46:58 IST 2012
0.00 user,
0.00 system,
0 status

So we see that in the above example, we tried to change the output format by using a different output format.

Resources
Since we discussed above that time utility displays information about the resource usage by a program, In this section
lets list the resources that can be tracked by this utility and the corresponding specifiers.
From the man page :
C Name and command line arguments of the command being timed.
D Average size of the processs unshared data area, in Kilobytes.
E Elapsed real (wall clock) time used by the process, in [hours:]minutes:seconds.
F Number of major, or I/O-requiring, page faults that occurred while the process was running. These are faults
where the page has actually migrated out of primary memory.
I Number of file system inputs by the process.
K - Average total (data+stack+text) memory use of the process, in Kilobytes.
M - Maximum resident set size of the process during its lifetime, in Kilobytes.
O - Number of file system outputs by the process.
P - Percentage of the CPU that this job got. This is just user + system times divided by the total running time. It
also prints a percentage sign.
R - Number of minor, or recoverable, page faults. These are pages that are not valid (so they fault) but which
have not yet been claimed by other virtual pages. Thus the data in the page is still valid but the system tables must
be updated.
S - Total number of CPU-seconds used by the system on behalf of the process (in kernel mode), in seconds.
U - Total number of CPU-seconds that the process used directly (in user mode), in seconds.
W - Number of times the process was swapped out of main memory.
X - Average amount of shared text in the process, in Kilobytes.
Z - Systems page size, in bytes. This is a per-system constant, but varies between systems.
c - Number of times the process was context-switched involuntarily (because the time slice expired).
e - Elapsed real (wall clock) time used by the process, in seconds.
k - Number of signals delivered to the process.
p - Average unshared stack size of the process, in Kilobytes.
r - Number of socket messages received by the process.
s - Number of socket messages sent by the process.
t - Average resident set size of the process, in Kilobytes.
w - Number of times that the program was context-switched voluntarily, for instance while waiting for an I/O
operation to complete.
x - Exit status of the command.
So we can see that there is a long list of resources whose usage can be tracked by the time utility.

Why /usr/bin/time? (Instead of just time)


Lets not use /usr/bin/time and use time instead.
http://www.thegeekstuff.com/2012/01/time-command-examples/

3/8

2/8/2014

Linux Time Command Examples

$ time -f "\t%U user,\t%S system,\t%x status" date


-f: command not found
real
user
sys

0m0.255s
0m0.230s
0m0.030s

As seen from the output above, the time command when used without the complete path (/usr/bin/time) spits out an
error regarding the -f flag. Also the format of output is neither the one specified by us in the command nor the default
format we discussed earlier. This lead to a confusion over how this output got generated.
When time command is executed without the complete path (/usr/bin/time), then its the built-in time command of the
bash shell that is executed.
Use man time to view the man page of /usr/bin/time
Use help time to view the information about the bash time built-in.
12

Tw eet

16

Like

> Add your comment

Linux provides several powerful administrative tools and utilities which will help you to
manage your systems effectively. If you dont know what these tools are and how to use them, you could be spending
lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand
system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..


1. 50 Linux Sysadmin Tutorials
2. 50 Most Frequently Used Linux Commands (With
Examples)
3. Top 25 Best Linux Performance Monitoring and
Debugging Tools
4. Mommy, I found it! 15 Practical Linux Find
Command Examples
5. Linux 101 Hacks 2nd Edition eBook

http://www.thegeekstuff.com/2012/01/time-command-examples/

Awk Introduction 7 Awk Print Examples


Advanced Sed Substitution Examples
8 Essential Vim Editor Navigation Fundamentals
25 Most Frequently Used Linux IPTables Rules
Examples
Turbocharge PuTTY with 12 Powerful AddOns

4/8

2/8/2014

Linux Time Command Examples

{ 2 comments read them below or add one }


1 Chris F.A. Johnson January 26, 2012 at 12:03 pm
The bash builtin time command is much more flexible than the binary. With it, you can time while and for loops,
grouped commands (in {}), builtin commands etc. You can format the ouput any way you like with the
TIMEFORMAT variable.
The binary can only time external commands.
2 Alex November 27, 2013 at 2:42 am
Great!!!
Leave a Comment
Name
E-mail
Website

Notify me of followup comments via e-mail


Submit

Previous post: How to Setup Rsyslog Remote Logging on Linux (Central Log Server)
Next post: C Pointer to Pointer, Pointer to Functions, Array of Pointers Explained with Examples
http://www.thegeekstuff.com/2012/01/time-command-examples/

5/8

2/8/2014

Linux Time Command Examples

RSS | Email | Twitter | Facebook | Google+


Search

COURSE
Linux Sysadmin CentOS 6 Course - Master the Tools, Configure it Right, and be Lazy

EBOOKS
Linux 101 Hacks 2nd Edition eBook - Practical Examples to Build a Strong Foundation in Linux
Bash 101 Hacks eBook - Take Control of Your Bash Command Line and Shell Scripting
Sed and Awk 101 Hacks eBook - Enhance Your UNIX / Linux Life with Sed and Awk
Vim 101 Hacks eBook - Practical Examples for Becoming Fast and Productive in Vim Editor
Nagios Core 3 eBook - Monitor Everything, Be Proactive, and Sleep Well

POPULAR POSTS
12 Amazing and Essential Linux Books To Enrich Your Brain and Library
50 UNIX / Linux Sysadmin Tutorials
50 Most Frequently Used UNIX / Linux Commands (With Examples)
How To Be Productive and Get Things Done Using GTD
30 Things To Do When you are Bored and have a Computer
Linux Directory Structure (File System Structure) Explained with Examples
Linux Crontab: 15 Awesome Cron Job Examples
Get a Grip on the Grep! 15 Practical Grep Command Examples
Unix LS Command: 15 Practical Examples
15 Examples To Master Linux Command Line History
Top 10 Open Source Bug Tracking System
Vi and Vim Macro Tutorial: How To Record and Play
Mommy, I found it! -- 15 Practical Linux Find Command Examples
15 Awesome Gmail Tips and Tricks
15 Awesome Google Search Tips and Tricks
RAID 0, RAID 1, RAID 5, RAID 10 Explained with Diagrams
http://www.thegeekstuff.com/2012/01/time-command-examples/

6/8

2/8/2014

Linux Time Command Examples

Can You Top This? 15 Practical Linux Top Command Examples


Top 5 Best System Monitoring Tools
Top 5 Best Linux OS Distributions
How To Monitor Remote Linux Host using Nagios 3.0
Awk Introduction Tutorial 7 Awk Print Examples
How to Backup Linux? 15 rsync Command Examples
The Ultimate Wget Download Guide With 15 Awesome Examples
Top 5 Best Linux Text Editors
Packet Analyzer: 15 TCPDUMP Command Examples
The Ultimate Bash Array Tutorial with 15 Examples
3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id
Unix Sed Tutorial: Advanced Sed Substitution Examples
UNIX / Linux: 10 Netstat Command Examples
The Ultimate Guide for Creating Strong Passwords
6 Steps to Secure Your Home Wireless Network
Turbocharge PuTTY with 12 Powerful Add-Ons

CATEGORIES
Linux Tutorials
Vim Editor
Sed Scripting
Awk Scripting
Bash Shell Scripting
Nagios Monitoring
OpenSSH
IPTables Firewall
Apache Web Server
MySQL Database
Perl Programming
Google Tutorials
Ubuntu Tutorials
PostgreSQL DB
Hello World Examples
C Programming
C++ Programming
DELL Server Tutorials
Oracle Database
VMware Tutorials
Ramesh Natarajan
Follow

About The Geek Stuff

http://www.thegeekstuff.com/2012/01/time-command-examples/

7/8

2/8/2014

Linux Time Command Examples

My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting


tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach
you or help you resolve a problem. Read more about Ramesh Natarajan and the blog.

Support Us
Support this blog by purchasing one of my ebooks.
Bash 101 Hacks eBook
Sed and Awk 101 Hacks eBook
Vim 101 Hacks eBook
Nagios Core 3 eBook

Contact Us
Email Me : Use this Contact Form to get in touch me with your comments, questions or suggestions about this
site. You can also simply drop me a line to say hello!.
Follow us on Google+
Follow us on Twitter
Become a fan on Facebook
Copyright 20082014 Ramesh Natarajan. All rights reserved | Terms of Service

http://www.thegeekstuff.com/2012/01/time-command-examples/

8/8

Вам также может понравиться