Time commands in linux

Linux / Unix: time Command Examples

I am new Linux and Unix systems user. How do I display the time of the execution of a command/script on Linux or Unix-like operating systems using shell prompt?

You need to use the time command to display the elapsed time during the execution of a command or script. This page explains time command in Linux and Unix-like system with most common examples.

time command details
Description Report time consumed by command execution
Category Processes Management
Difficulty Easy
Root privileges No
Est. reading time 4 minutes
Table of contents
  • » Syntax
  • » Examples
  • » Redirect time output to file
  • » GNU/Linux time command
  • » Understanding TIMEFORMAT
  • » Summing up
  1. real time
  2. user time
  3. sys time

Purpose

Run command/programs or script and summarize system resource usage on your screen.

Syntax

The basic syntax is as follows:
time command
time command arg1 arg2 . argN
time [options] command arg1 arg2 . argN

Is my time command shell built-in or external command?

The time command is also built into the BASH/KSH/CSH/TCSH with a different syntax. Let us use the type command or command command to verify location:
type -a time
command -V time

To run the time command while in the shells, type:
/usr/bin/time -p command
/bin/time -p command arg1 arg2
Users of the bash or ksh shell need to use an explicit path in order to run the external time command and not the shell builtin variant.

time command examples

To measure the time required to run a program called date, enter:
$ time date
To use external time command give full path to time binary:
$ /usr/bin/time -p date

How do I redirect time command output to a file?

The syntax is as follows to save a record of the time command information in a file called output.time.txt, run:

If above command failed, try the following to save a record of the time command information in a file:

Use the cat command to display output on screen:
$ cat output.time.txt

A note about GNU/Linux time command

GNU/Linux user can use the following syntax to write the resource use statistics to file instead of to the standard error stream:
$ /usr/bin/time -o output.time.txt -p date
$ cat output.time.txt
Pass the -a option to append the resource use information to the output file instead of overwriting it. This option is only useful with the -o option:
$ /usr/bin/time -a -o output.time.txt -p sleep 2
$ cat output.time.txt

Using time command on my Linux system

Use FORMAT as the format string that controls the output of time

Table 1: Formatting the output of /usr/bin/time
FORMAT Description
% A literal `%’.
C Name and command line arguments of the command being timed.
D Average size of the process’s 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 System’s 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.

Using time command on Linux or Unix with formatting

In this example, show just the user, system, and total time using format option:
$ /usr/bin/time -f «%E real,%U user,%S sys» sleep 2
$ /usr/bin/time -f «%E real,%U user,%S sys» /path/to/script
Sample outputs:

See percentage of CPU used by your command:
/usr/bin/time -f «CPU Percentage: %P» command
/usr/bin/time -f «CPU Percentage: %P» grep vivek /etc/passwd
/usr/bin/time -f «CPU Percentage: %P» find /etc/ -type f -iname «a*.conf»
CPU Percentage: 76%

Understanding TIMEFORMAT used by bash’s buitin time

The value of TIMEFORMAT parameter is used as a format string specifying how the timing information for pipelines prefixed with the time reserved word should be displayed. The % character introduces an escape sequence that is expanded to a time value or other information. The escape sequences and their meanings are as follows; the braces denote optional portions.

Table 2: TIMEFORMAT parameter values

Value Description
%% A literal %.
%[p][l]R The elapsed time in seconds.
%[p][l]U The number of CPU seconds spent in user mode.
%[p][l]S The number of CPU seconds spent in system mode.
%P The CPU percentage, computed as (%U + %S) / %R.

The optional p is a digit specifying the precision, the number of fractional digits after a decimal point. A value of 0 causes no decimal point or fraction to be output. At most three places after the decimal point may be specified; values of p greater than 3 are changed to 3. If p , is not specified, the value 3 is used. The optional l specifies a longer format, including minutes, of the form MMmSS.FFs . The value of p determines whether or not the fraction is included.

How to use bash’s builtin time command in Linux / Unix

The syntax is almost same:
time command
time ls
TIMEFORMAT=»%P» time ls
TIMEFORMAT=»%U» time sleep 3
TIMEFORMAT=»%S» time sleep 3
TIMEFORMAT=»%R» time sleep 2

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Summing up

You learned about the time command that reports the time consumed by the pipeline’s execution. It will summarize system resource usage on the screen. The syntax of time might change on your local Unix or Linux distro. Hence, to get help for builtin time command, type:
help time
# or read shell man page
man bash
For external time (/bin/time or /usr/bin/time) command, run:
man time

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to Time a Command in Linux

If you want to be a good Linux administrator, therefore you should know time command. It is used to determine how long a given command takes to run.

It is useful for testing the performance of your scripts and commands (ie helps to find execution time for shell scripts or time taken for a command to finish).

How to Use Linux time command

To use time command, just execute time with the command/program you want to run as input. Please check below example

The real signifies the wall clock time the ‘ping’ command took from execution till termination, user and sys are the time taken by ping the user space and kernel space.

How to Make time command writes its output to a file

To write the time command output to a file instead of the print out screen, use the -o command line option, which expects a file name/path as input.

Now we will display ping’s output on stdout, while the time command output will be written to the text file.

Note: We used /usr/bin/time instead of time because the shell built-in time command doesn’t offer the -o option.

How to appends its output to an existent file

To append the time command output to an existent file instead of overwrite it, use the -a command line option.

How to get Detailed output of linux time command

We can use the -v command line option to produce detailed output.

How to customizing time command output

There are a large number of formatting options as shown in the following list

C — Name and command line arguments used
D — Average size of the process’s unshared data area in kilobytes
E — Elapsed time in a clock format
F — Number of page faults
I — Number of file system inputs by the process
K — Average total memory use of the process in kilobytes
M — Maximum resident set the size of the process during the lifetime in Kilobytes
O — Number of file system outputs by the process
P — Percentage of CPU that the job received
R — Number of minor or recoverable page faults
S — Total number of CPU seconds used by the system in kernel mode
U — Total number of CPU seconds used by user mode
W — Number of times the process was swapped out of main memory
X — Average amount of shared text in the process
Z — System’s page size in kilobytes
c — Number of times the process was context-switched
e — Elapsed real 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 time the process was context-switched voluntarily
x — Exit status of the command

We can use the formatting switches as follows:

The output for the above command would be something like this:

If we want to add a new line as part of the format string use the newline character as follows:

Linux Time Command Versions

There are three-time command Versions, Bash, Zsh and Gnu time command. We can use the type command to determine whether time is a binary or a built-in keyword.

Please write your suggestions or comments on time command and for more information use time man pages.

Источник

12 UNIX / Linux Time Command Output Format Option Examples

Linux time command is helpful to identify the time taken by a command.

Using Linux time command, you can figure out how much time was taken to execute a command, or shell script, or any other program.

By default, time command executes the given command or program. After execution, it displays the statistics and resources usage on the standard error.

Time command provides several command line options and various format options as explained in this tutorial

1. Basic Time Command Usage Example

Time command syntax:

For example, time command is run on sleep command without any option.

Let’s now understand some important command line options of this command.

2. Write Time Statistics Output to a File using -o option

This option is to restrict sending the command results to standard error, but writes results into output file. This option overwrites the specified file.

Here is an example :

3. Append Time Statistics Output to an Existing File using -a option

This option allows appending time command output into file. It is used along with -o option. This option avoids overwriting of content of output file by appending time command output into specified output file.

Here is an example :

4. Display Percentage of CPU used – %P

You can provide output formatting choices using -f option. This option allows user to provide output formatting options. It possibly does overriding the format specified in the environment variable TIME. Following formatting options are described below – %P, %M, %S, %e, %E, %C, %Z, %c, %x.

This option gives percentage of the CPU that the process of command (i.e. run under time command) has got for its execution. This is just user + system times divided by the total running time. It also prints a percentage sign.

Here, command output shows that find command took 82% of CPU.

5. Display Maximum Resident Set Size – %M

This option gives maximum resident set size of the process of command (i.e. run under time command) during its lifetime, in Kilobytes.

Here, command output shows that find command took 8688 KB as maximum resident size of process.

6. Display Total Number of CPU-seconds – %S

This option gives total number of CPU-seconds used by the system on behalf of the process (in kernel mode), in seconds.

Here, command output shows that find command took 0.35 CPU-seconds in kernel mode.

7. Display Elapsed Real Time in Seconds – %e

This option gives elapsed real time (i.e. wall clock) used by the process, in seconds.

Here, command output shows that sleep command execution elapsed till 2 seconds.

8. Display Elapsed Real Time in Different Format – %E

This option gives elapsed real time (i.e. wall clock) used by the process, in this format – [hours:]minutes:seconds.

Here, command output shows that sleep command execution took 0 hour, 0 minute and 2 seconds.

9. Display Program Name and Command Line Arguments – %C

This option gives name and command line arguments of the command (i.e. run under time command).

Here, time command output shows name of command being run and its command line arguments.

10. Display System Page Size in Bytes – %Z

This option gives system’s page size, in bytes. This is a per-system constant, but it may vary one system to another system.

Here, command output shows that sleep command used 4096 bytes as system page size.

11. Display Number of Context Switches – %c

This option gives number of times the process was context-switched involuntarily (because the time slice expired).

Here, command output shows that 254 times context switching of process took place during execution of find command under time command.

12. Display Exit Status of a Command – %x

This option gives exit status of the command (i.e. run under time command).

Here, command output shows that top1 command is failed because this tope1 as a file does not exist.

As per man page of time command, exit status of time command can be following:

  • If command specified to time command was invoked, the exit status is the exit status of command which is run with time command.
  • It is 127 if command specified to time command could not be found.
  • 126 if command specified to time command could be found but could not be invoked.
  • Some other non-zero value (1-125) if something else went wrong.

Finally, there is a difference between executing just “time” and “/usr/bin/time”. We explained this in our earlier introduction to time command article.

Источник

Читайте также:  Samsung easy wireless setup windows
Оцените статью