Sometimes when working with the Linux terminal you need to know the time of execution of certain commands, for example, to keep track of problems. There is a utility called time
for this purpose. We will tell you how the Linux time
command works and what its syntax is. Then we will move on to the description of the available options. And at the end we will mention some popular usage scenarios.
The utility runs a user-defined command and then displays information about its execution time. Its syntax is quite convenient. First you need to specify the options for time
, then the command to be executed, and at the end - the arguments to it:
$ time [options] command_to_execute [arguments]
Let's consider the list of available options:
-o
, --output
- save data to the selected file instead of the standard output in the terminal. The old data in the file will be overwritten.-a
, --append
- add new information to the file instead of overwriting the old one. The option is only useful in conjunction with -o
.-f
, --format
- select a specific output format. Details about formatting are described in the next section of this article.-p
, --profitably
- use a data output format to conform to the POSIX 1003.2 standard.-v
, --verbose
- output detailed information about program execution.-V
, --version
- output the version of the time
utility.This list contains only the basic options. You can get detailed information with the command:
$ man time
By default , time
can output information in a form that is not comfortable for reading,
It is desirable to set output formatting options for it, which will be discussed now. There are three of them in total. Let's take the apt-get update
command as an example.
The -v
option is used to output detailed information:
$ sudo time -v apt-get update
The -p
option is needed to output data in a POSIX 1003.2 compliant format:
$ sudo time -p apt-get update
The formatting data line typically includes resource specifiers and plain text. The %
sign indicates that the character following it should be treated as a resource specifier.
The sign is used to specify the delimiter character. There are three available options:
t
- tab, n
- newline, \
- backslash. If you specify any other character after , the terminal will show a question mark (
?
), which indicates an input error.
The rest of the text in the format string is completely copied into the output field. In this case, time
always starts outputting data from a new line after the information about the execution of the command itself.
Let's consider the available resource specifiers:
%
- literal %. i.e. to output a percent sign you should specify %% in the command.C
- command name and arguments used.D
- average size of the unshared data area. It is displayed in kilobytes.E
- real time of command execution in the usual hour format. It is displayed as [Hours:]<imutes:seconds
.N
- number of major or I/O errors that occurred during the process execution.I
- number of file system accesses.K
- average value of memory used for code(text
), initialized data(data
) andstack (stack
). It is displayed in kilobytes.M
- maximum size of the resident set during process execution in kilobytes.O
- number of file system exits.P
- percentage of CPU (central processing unit) load.R
- number of minor errors.S
- time in seconds during which the CPU was used by the system on behalf of the process in supervisor mode(kernel
mode).U
- time in seconds during which the CPU was used by the process directly inuser
mode.W
- how many times the process was unloaded from RAM.Z
- system page size. This value is a constant, but it varies between systems.c
- number of involuntary context switches during process execution.e
- actual command execution time in the usual hourly format. It is displayed in seconds.k
- number of signals that reached the process.p
- average size of the unshared stack of the process, in kilobytes.r
- number of received socket messages.s
- number of socket messages sent.t
- average size of the process's resident set, in kilobytes.w
- number of voluntary context switches during process executionx
- return code for the command.These were all the resource specifiers used when choosing the formatting for time
. Now let's move on to the scenarios of using the command.
Let's look at three basic examples that are quite commonly used: output to the terminal, output to a separate file, and output with formatting.
As an example we will take the sleep command, which pauses for a specified time. It will be a very clear example, because the pause time will coincide with the command execution time in time
:
$ time sleep 3
Time is written immediately in three columns. Let's go through each of them:
real
- the total time from the start of the process execution to its completion.
user
- the time during which the process was running inuser
mode.
sys
- the time during which the process was involved in supervisor mode(kernel
mode).
The next useful scenario is to output the time it takes to load a web page header via the curl
utility. Let's take our website as an example:
$ time curl -I https://losst.ru
We should mention a situation where executing the command puts the terminal window into a different mode, such as when you run the nano editor to modify the /home/root-user/script.txt
file:
$ time nano /home/root-user/scripts/main_script.txt
After the editor finishes, you will see the total time you worked on the file.
Now you know how to see the execution time of a Linux command.
The information about the results of the time command can be saved to a separate file using the-o
option. In this case, they will not be displayed in the terminal window. Let's take the example of extracting the contents of the ~/data/data.tar.gz
archive using the tar
utility. Let's save information about the execution time in a new file ~/data/data/data_time.txt.
We will also add the-v option
to the command to get detailed information:
$ sudo time -v -o ~/data/data_time.txt tar -xvf ~/data/data.tar.gz -C ~/data
The file will have the following contents.
If you use the -o
option, remember that it overwrites the old information in the output file with the new information. This is applicable when creating new files, but is not suitable for logging. With the optional -a option, overwriting the content is replaced by adding new information:
$ sudo time -v -a -o ~/data/data_time.txt tar -xvf ~/data/data.tar.gz -C ~/data
The original option of formatting data is not suitable in all cases. As an example, we will take the previously used command for extracting an archive, but we will not save the data to a file. When executing it, we are interested in:
specifier%C
. specifier%P
. specifier%e
.For comfortable perception, each item will be displayed in a separate line using the separator n
. At the same time, they will all be signed in an understandable way.
Here is how the final version of the command looks like:
sudo time -f "Command info:n%CnCPU usage: %PnExecution time: %e sec" tar -xvf ~/data/data.tar.gz -C ~/data
All text except for the characters after %
is fully displayed in the terminal. This is a convenient way of marking up