In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to check the execution time of commands or processes in Linux. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Find the execution time of a command or process in Linux
To measure the execution time of a command or program, run:
$/ usr/bin/time-p ls
Or
$time ls
Sample output:
Dir1 dir2 file1 file2 mcelogreal 0m0.007suser 0m0.001ssys 0m0.004s $time ls-a. .bash _ logout dir1 file2 mcelog .sudo _ as_admin_successful.. .bashrc dir2 .gnupg .profile. Wget-hsts.bash_history .cache file1 .local .stackreal 0m0.008suser 0m0.001ssys 0m0.005s
The above command shows the total execution time of the ls command. You can replace ls with any command or process to find the total execution time.
Output details:
Real-refers to the total time spent by a command or program
User-refers to the time spent by the program in user mode
Sys-refers to the time spent by a program in kernel mode
We can also limit the command to run only for a period of time. Refer to the following tutorial for more details:
How to make a command run for a specific length of time in Linux
Time and / usr/bin/time
You may have noticed that we used two commands time and / usr/bin/time in the above example. So, you might want to know the difference between them.
First, let's use the type command to see what the time command is. For those Linux commands that we don't know about, the type command is used to find information about the commands. Refer to this guide for more details.
$type-a timetime is a shell keywordtime is / usr/bin/time
As you can see in the output above, time is two things:
One is the built-in keyword in BASH shell
One is an executable file, such as / usr/bin/time
Because the shell keyword takes precedence over the executable, when you run the time command without giving the full path, you are running the shell built-in command. However, when you run / usr/bin/time, you are running the real GNU time command. Therefore, in order to execute the real command, you may need to give the full path.
In most shell, such as BASH, ZSH, CSH, KSH, TCSH, etc., the built-in keyword time is available. The time keyword has fewer options than the executable, and the only option you can use is-p.
You now know how to use the time command to find the total execution time of a given command or process. Want to learn more about GNU time tools? Keep reading!
A brief introduction to GNU time Program
The GNU time program runs a command or program with a given parameter and summarizes the system resource usage to standard output after the command is completed. Unlike the time keyword, a GNU time program displays not only the execution time of a command or process, but also other resources such as memory, Icano, and IPC calls.
The syntax of the time command is:
/ usr/bin/time [options] command [arguments...]
Options in the above syntax refers to a set of options that can be used with the time command to perform a specific function. The available options are given below:
-f,-format-- use this option to specify the output format as needed.
-p,-portability-- use a brief output format.
-o file,-output=FILE-- writes the output to the specified file instead of to standard output.
-a,-append-- appends the output to the file instead of overwriting it.
-v,-verbose-- this option displays details of the output of the time command.
-quiet-this option prevents the time command from reporting the status of the program.
When you use the GNU time command without any options, you will see the following output.
$/ usr/bin/time wc / etc/hosts9 28 273 / etc/hosts0.00user 0.00system 0:00.00elapsed 66%CPU (0avgtext+0avgdata 2024maxresident) k0inputs+0outputs (0major+73minor) pagefaults 0swaps
If you run the same command with the shell keyword time, the output will be a little different:
$time wc / etc/hosts9 28 273 / etc/hostsreal 0m0.006suser 0m0.001ssys 0m0.004s
Sometimes, you may want to output system resource usage to a file rather than to a terminal. To do this, you can use the-o option, as shown below.
$/ usr/bin/time-o file.txt lsdir1 dir2 file1 file2 file.txt mcelog
As you can see, the time command is not displayed on the terminal. Because we wrote the output to the file.txt file. Let's take a look at the contents of this file:
$cat file.txt0.00user 0.00system 0:00.00elapsed 66%CPU (0avgtext+0avgdata 2512maxresident) k0inputs+0outputs (0major+106minor) pagefaults 0swaps
When you use the-o option, if you don't have a file named file.txt, it will create one and write the output in. If the file exists, it overwrites the original contents of the file.
You can use the-an option to append the output to the file instead of overwriting its contents.
$/ usr/bin/time-a file.txt ls
The-f option allows users to control the output format according to their preferences. For example, the output of the following command shows only the user, system, and total time.
$/ usr/bin/time-f "\ t% E real,\ t% U user,\ t% S sys" lsdir1 dir2 file1 file2 mcelog0:00.00 real, 0.00 user, 0.00 sys
Note that the built-in time command in shell does not have all the functions of the GNU time program.
A detailed description of the GNU time program can be viewed using the man command.
$man time
To learn more about the Bash built-in time keyword, run:
$help time
On how to check the command or process execution time in Linux to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.