Linux time 命令新手教程(附实例)
在此页
- Linux时间命令
- Q1。如何使用时间命令?
- Q2。如何让时间将其输出写入文件?
- Q3。如何让时间产生详细的输出?
- Q4。如何自定义时间命令输出?
- 结论
有时,当您执行一个程序时,您可能想知道它的系统资源使用情况。比如进程在内核模式和用户模式下花费了多少时间,以及其他信息。
值得庆幸的是,有一个工具——被称为时间——是专门为此目的而构建的。在本文中,我们将使用一些易于理解的示例来讨论时间命令的基础知识。
但在此之前,值得一提的是,本教程中的所有示例都已在 Ubuntu 18.04 LTS 机器上进行了测试。
Linux时间命令
Linux 中的 time 命令允许您运行程序并汇总它们的系统资源使用情况。以下是它的语法:
time [OPTIONS] COMMAND [ARGS]
以下是工具手册页对其的描述:
time run the program COMMAND with any given arguments ARG.... When COMMAND finishes, time displays
information about resources used by COMMAND (on the standard error output, by default).
If COMMAND exits with non-zero status, time displays a warning message and the exit status.
time determines which information to display about the resources used by the COMMAND from the
string FORMAT. If no format is specified on the command line, but the TIME environment variable
is set, its value is used as the format. Otherwise, a default format built into time is used.
Options to time must appear on the command line before COMMAND. Anything on the command line after
COMMAND is passed as arguments to COMMAND.
以下是一些 Q&A 风格的示例,可以让您更好地了解时间命令的工作原理。
Q1。如何使用时间命令?
基本用法很简单 - 只需将要运行的命令/程序作为输入执行时间。
例如,我按以下方式使用时间命令:
time ping linux教程
这是输出:
PING linux教程 (104.24.0.68) 56(84) bytes of data.
64 bytes from 104.24.0.68 (104.24.0.68): icmp_seq=1 ttl=59 time=93.8 ms
64 bytes from 104.24.0.68 (104.24.0.68): icmp_seq=2 ttl=59 time=91.5 ms
64 bytes from 104.24.0.68 (104.24.0.68): icmp_seq=3 ttl=59 time=93.1 ms
64 bytes from 104.24.0.68 (104.24.0.68): icmp_seq=4 ttl=59 time=102 ms
^C
--- linux教程 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 91.510/95.249/102.490/4.267 ms
real 0m3.472s
user 0m0.000s
sys 0m0.004s
输出中的最后三行由 time 命令添加。 real 表示 ping 命令从执行到终止所用的挂钟时间,而 user 和 sys 分别是 ping 用户空间和内核空间所用的时间。有关这三个时间的详细信息,请访问此处。
Q2。如何让时间将其输出写入文件?
如果您希望 time 命令将其输出写入文件而不是终端,请使用 -o 命令行选项,该选项需要文件名/路径作为输入。
例如:
/usr/bin/time -o /home/himanshu/time-output.txt ping linux教程
此命令将在 stdout 上显示 pings 输出,而 time 命令输出将写入文本文件。
注意:我们使用 /usr/bin/time 而不是时间,因为 shell 内置时间命令不提供 -o 选项。
默认情况下,每次运行此命令时,输出文件都会被覆盖。但是,如果需要,可以使用 -a 命令行选项确保附加新输出。
Q3.如何让时间产生详细的输出?
这可以使用 -v 命令行选项来完成。例如,当我在运行时使用 ping 命令使用此选项时,时间命令输出中会生成以下详细信息:
Command being timed: "ping linux教程"
User time (seconds): 0.00
System time (seconds): 0.00
Percent of CPU this job got: 0%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:11.77
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 3064
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 158
Voluntary context switches: 14
Involuntary context switches: 0
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
所以你可以看到 time 命令在这种模式下产生了很多其他细节。
Q4.如何自定义时间命令输出?
time 命令还提供了一个 format 命令行选项,可让您自定义此工具的输出。它提供了一组资源说明符,您可以使用它们来获取 time 命令支持的任何类型的信息(请参阅上一节)。
例如下面执行中的时间命令:
/usr/bin/time -f "\t%C [Command details],\t%K [Total memory usage],\t%k [Number of signals process received]" ping linux教程
产生了这个输出:
ping linux教程 [Command details], 0 [Total memory usage], 0 [Number of signals process received]
time 命令手册页包含与 format 命令行选项相关的详细信息。
结论
时间命令主要由软件开发人员和测试人员使用。然而,即使您不是其中一员,知道它也没有什么害处,因为您永远不知道什么时候可能需要它。可以通过工具手册页访问有关此命令的更多信息。