对Linux命令运行时间进行监测

在工作中,我曾写过一个 Shell 脚本,这个脚本可以从 4 个 NTP 服务器轮流获取时间,然后将最可靠的时间设置为系统时间。因为我们对于时间的要求比较高,需要在短时间内就获取到正确的时间。所以我们就需要对这个脚本运行时间进行测试,看看从开始运行到正确设置时间需要花费多少时间。

其实在工作中,还有很多情况下需要测试一个脚本或者程序运行多少时间,特别是对于时间性要求比较高的系统更是如此。对于时间的测试,我们可以用到一个命令:time 。下面我们就详细看看如何使用 time 命令来对脚本/命令进行测时。

1. time 命令基本用法

time 命令最基本的用法,就是 time + 命令 ,比如:

$ time ping baidu.com
PING baidu.com (123.125.114.144) 56(84) bytes of data.
64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=1 ttl=56 time=2.83 ms
64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=2 ttl=56 time=2.77 ms
…………
^C
--- baidu.com ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 10818ms
rtt min/avg/max/mdev = 2.765/2.808/2.862/0.039 ms

real    0m11.173s
user    0m0.004s
sys     0m0.002s

在结果里,real 表示从我们执行 ping 命令到最终按 ctrl+c 终止这段时间所耗费的时间;user 及 sys 分别表示 ping 命令在用户空间及内核空间所运行的时间。

2. 将时间信息写入文件

如果我们想把时间信息直接写入到文件,而不是显示在屏幕上,那么我们可以使用 -o 选项,并指定写入的文件路径。

$ /usr/bin/time -o /home/alvin/time-output.txt ping baidu.com

执行这个命令后,ping 命令的输出结果依然会在终端里,而 time 命令的结果就写入到我们所指定的 time-output.txt 文件里。

-o 选项表示输出文件不存在就创建,如果存在的话就直接覆盖重写。如果我们不想覆盖重写,而是想追加在文件后面,我们可以使用 -a 选项。

$ /usr/bin/time -a /home/smart/time-output.txt ping linoxide.com

3. 显示更详细的时间信息

time 命令不带选项的话,显示的信息量比较少,如果我们想获得更详细的信息,那么我们可以使用 -v 选项。

$ /usr/bin/time -v ping baidu.com
PING baidu.com (123.125.114.144) 56(84) bytes of data.
64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=1 ttl=56 time=2.75 ms
64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=2 ttl=56 time=2.76 ms
64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=3 ttl=56 time=2.85 ms
64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=4 ttl=56 time=2.77 ms
^C
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3300ms
rtt min/avg/max/mdev = 2.751/2.785/2.851/0.075 ms
       Command being timed: "ping baidu.com"
       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:03.64
       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): 2140
       Average resident set size (kbytes): 0
       Major (requiring I/O) page faults: 0
       Minor (reclaiming a frame) page faults: 626
       Voluntary context switches: 10
       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

这个结果信息就相当详细了,我们可以获取到足够多我们所需要的信息。

4. 自定义输出格式

默认情况下,time 命令只输出 real,usr,sys 三个内容,如果我们想要个性化一些,算定义它的输出格式,time 命令也是支持的。time 命令支持的格式有很多,如下所示:

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

如果我们想要输出以下这样的格式:

Elapsed Time = 0:01:00, Inputs 2, Outputs 1

我们可以这样自定义:

$ /usr/bin/time -f "Elapsed Time = %E, Inputs %I, Outputs %O" ping baidu.com
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=54 time=1.82 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=54 time=1.86 ms
^C
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 1.825/1.859/1.879/0.056 ms
Elapsed Time = 0:03.92, Inputs 0, Outputs 0

如果你想让输出的结果有换行,可以在对应的地方添加 \n ,比如:

$ /usr/bin/time -f "Elapsed Time = %E \n Inputs %I \n Outputs %O" ping baidu.com

这样输出的结果就类似于这样:

Elapsed Time = 0:03.92
Inputs 0
Outputs 0

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/206445.html<

(0)
运维的头像运维
上一篇2025-04-08 02:14
下一篇 2025-04-08 02:15

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注