Linux time command example

The “time” Command in Linux [4 Practical Examples]

The time command in Linux measures the amount of time it takes to complete a command or a script. This command is very useful for analyzing the resource utilization and performance of the system in Linux. In this section, I will demonstrate the ins and outs of the time command in Linux.

A. Description

  • Real-Time: The real-time is the actual amount of time that has passed while the command was running.
  • User Time: The user time is the amount of CPU time spent by the CPU while executing the commands in user space.
  • System Time: The system time is the amount of CPU time that was spent by the CPU while executing the commands in system mode or kernel mode.

B. Syntax

The time command in Linux has a very simple syntax. And the syntax is indicated below:

Note: In the syntax above, OPTION is enclosed by a square bracket followed by 3 dots representing that more than one option can be used simultaneously. Moreover, COMMAND suggests that you can specify one command at a time.

C. Options

A few options are available for the time command. I have mentioned the most used options of the command here. However, you can explore the man page for the time command to know more about its options.

Useful Options

  • -p, Displays time in POSIX format.
  • -o, Stores the output in the text file.

Note: The options in Linux CLI (Command Line Interface) are case-sensitive, so be careful while using them.

Practical Examples of the “time” Command in Linux

The time command measures the time of running a particular command or bash script and displays that on the terminal screen. Here you will learn some practical examples of the time command in Linux.

Example 1: Time Measurement of Running the “ls” Command Using the “time” Command in Linux

The time of running a specific command can be measured using the time command. Here I want to measure the time of running the ls command in Linux. Follow the process below to measure time:

Steps to Follow:

➊ Initially open the Terminal in Ubuntu.

➋ Execute the command below in the command prompt:

➌ Then, tap the ENTER button.

Time Measurement of Running the “ls” Command Using the “time” Command in Linux

As you can see, the contents of the current directory are listed in the output and the time of running the ls command is shown as well.

Читайте также:  Linux graphics in console

Example 2: Time Measurement of Running the Bash Script

You can measure the time of running a bash script in Linux. Here I want to measure the time of running the script.sh in Linux. You can follow the steps below to measure time:

Steps to Follow:

➊ To start, launch the Ubuntu Terminal.

➋ Copy the command below in the command prompt:

➌ Now, hit the ENTER key.

Time Measurement of Running the Bash Script

As you can see in the image below, the output is showing the time of running the script.sh.

Example 3: Display Time in POSIX Format Using the “time” Command in Linux

POSIX stands for Portable Operating System Interface. It stores time in seconds. You can use the time command with option -p in Linux to display the time in POSIX format. Here I want to determine the time of running the sleep command in Linux. To determine the time you can follow the instructions below:

Steps to Follow:

➊ Launch the Ubuntu Terminal.

➋ Type the following command in the command prompt:

➌ After that, strike the ENTER button.

Display Time in POSIX Format Using the “time” Command in Linux

In the image below, the output is showing that it took 5 seconds(as determined) to run the sleep command in Linux.

Example 4: Display the Time in the Text File Rather than the Terminal Screen

If you want to display the time in the text file rather than the terminal screen, you can also do this by using the time command with the option -o. Do follow the procedure below to display time in a text file:

Steps to Follow:

➊ To initiate open the Ubuntu Terminal.

➋ Run the command below in the command prompt:

/usr/bin/time -o time.txt sleep 5

➌ Then, press the ENTER button.

❹ Now execute the following command in the command prompt:

❺ After that, hit the ENTER key.

Display the Time in the Text File Rather than the Terminal Screen

In the following image, you can see the time in the text file.

Conclusion

As exemplified in this article, the time command has some practical uses in Linux. You’ve also got to know the syntax, some functional options, and the practical applications of this command. To master Linux, practice the command and its practical applications thoroughly.

Similar Readings

  • The “tty” Command in Linux [4 Practical Examples]
  • The “uptime” Command in Linux [5 Practical Examples]
  • The “vmstat” Command in Linux [6 Practical Examples]
  • The “service” Command in Linux [6 Practical Examples]
  • The “sestatus” Command in Linux [4 Practical Examples]
  • The “shutdown” Command in Linux [7 Practical Examples]

Источник

Understanding the Time Command

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

In Linux, we can use the time command to measure the program’s execution time.

Читайте также:  Linux get all disks

In this tutorial, we’ll take a look at how to use time and how to read its output.

We tested our examples in Bash and should also work in other POSIX-compatible shells unless otherwise noted.

2. Measuring Execution Time

We can measure execution time by prefixing any command with time:

This executes the program named “command” and outputs its run time:

real 0m2.998s user 0m0.063s sys 0m1.616s

The output consists of three components. Let’s go into these in more detail.

3. Output

3.1. User Time

In Linux, commands can are either executed in user or system mode. Another name for system mode is kernel mode.

Some commands require direct and unrestricted access to the underlying hardware. These are executed in kernel mode. All other commands are executed in user mode.

user time represents the time that a command has executed in user mode. In our case, that was precisely 0.063 seconds.

3.2. System Time

Our command spent 1.616 seconds running in the system time mode. sys represents the time the executed command has spent running in system or kernel mode.

3.3. Real-Time

Real-time represents the actual time that elapsed while the command ran. This is the time that passed between pressing the enter key and termination of the program.

In the example above, it took our command 0 minutes and 2.998 seconds to execute. This is actually bigger than the sum of times spent in user and system mode. That’s because we’re running our command in a concurrent environment where Linux has to balance resources between multiple processes.

For multi-threaded processes that run on multiple CPU cores, real time measured might be smaller than the total time spent in user and system mode as reported by the individual threads.

4. Formatting

By default, time formats its output to show hours, minutes and seconds, for example:

Because most programs’ execution time is below 1 hour, time will omit the hour part for execution times under 60 minutes.

With the GNU 1.7 version of time, we can format the output by use of the -f parameter that takes a formatting string. With a custom format we can print a lot of other information as well, for example:

This will not only print the real execution time (%E) but also the number of times the process was swapped out of main memory (%W) and the number of socket messages the process received (%r).

A full list of all formatting options is available on the man page.

The GNU 1.7 version of time however is not POSIX compatible. Most shells, like Bash, ship with a POSIX-compatible version that doesn’t have this formatting option.

5. Conclusion

In this article, we’ve learned how to interpret the output of time. Each program we run spends time running in user or system mode or both.

Real represents the actually elapsed time from start to finish of our program – it might be smaller than the added totals of time spent in user and system mode.

The GNU 1.7 version of time comes with a lot of formatting options. However, most shells don’t ship with this version by default as it isn’t POSIX-compatible.

Читайте также:  Установить значение переменной окружения linux

Источник

How to use time command on Linux

The time command is a very simple, but useful command line utility in Linux. Essentially, you can think of it as a stopwatch built into the terminal, as it measures the amount of time it takes to execute a specified Linux command.

In this guide, we’ll show you how to use the time command through various examples, and teach you how to interpret its output. We’ll also show how to use GNU time, which is different than the time utility built into the Bash and zsh shells.

In this tutorial you will learn:

  • How to use time command
  • How to use GNU time
  • How to interpret the output of the time and GNU time commands

time command on Linux

Software Requirements and Linux Command Line Conventions

Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software time
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

time vs GNU time

We’ll be discussing two different versions of time in this article, the default time for the Bash shell, and GNU time. Note that there are also other versions of time used on other shells, such as zsh. For the purposes of this guide, we’ll assume you’re using the Bash shell.

To call the Bash time utility, you can simply type the time command. To use GNU time, specify the full path which should be /usr/bin/time .

$ time (command here) OR $ /usr/bin/time (command here)

time and GNU time examples

In the following example, we’ll use both utilities to measure the time it takes to download a file with wget.

Using Bash time command to measure the time it takes to download a file

The part we want to pay attention to is the last three lines, which were output by time .

real 4m12.067s user 0m0.086s sys 0m1.030s

Here’s what this information means:

  • real – the actual amount of time it took to run the command
  • user – the amount of time the CPU spent in user mode
  • sys – the amount of time the CPU spent in kernel mode

And now let’s try the same download, while measuring with GNU time:

Running the same download but measuring with GNU time

We’ll only concern ourselves with the last two lines – the ones from GNU time.

0.05user 0.95system 0:08.64elapsed 11%CPU (0avgtext+0avgdata 7220maxresident)k 0inputs+30488outputs (0major+428minor)pagefaults 0swaps

This outputs the same information as time , along with some more detailed statistics, and a very human-readable measurement of the CPU usage.

Closing Thoughts

In this guide, we saw how to use the time and GNU time command line utilities on Linux to measure the amount of time and CPU usage it takes to execute any command we want. These commands are very simple to master, but can come in handy quite often.

Comments and Discussions

Источник

Оцените статью
Adblock
detector