经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Linux/Shell » 查看文章
linux 时间命令
来源:cnblogs  作者:码农先锋  时间:2021/1/25 11:04:39  对本文有异议

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

Linux命令运行时间测试Linux命令运行时间测试

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

1. time 命令基本用法

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

  1. $ time ping baidu.com
  2. PING baidu.com (123.125.114.144) 56(84) bytes of data.
  3. 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=1 ttl=56 time=2.83 ms
  4. 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=2 ttl=56 time=2.77 ms
  5. …………
  6. ^C
  7. --- baidu.com ping statistics ---
  8. 8 packets transmitted, 8 received, 0% packet loss, time 10818ms
  9. rtt min/avg/max/mdev = 2.765/2.808/2.862/0.039 ms
  10. real 0m11.173s
  11. user 0m0.004s
  12. sys 0m0.002s

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

2. 将时间信息写入文件

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

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

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

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

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

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

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

  1. $ /usr/bin/time -v ping baidu.com
  2. PING baidu.com (123.125.114.144) 56(84) bytes of data.
  3. 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=1 ttl=56 time=2.75 ms
  4. 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=2 ttl=56 time=2.76 ms
  5. 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=3 ttl=56 time=2.85 ms
  6. 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=4 ttl=56 time=2.77 ms
  7. ^C
  8. --- baidu.com ping statistics ---
  9. 4 packets transmitted, 4 received, 0% packet loss, time 3300ms
  10. rtt min/avg/max/mdev = 2.751/2.785/2.851/0.075 ms
  11. Command being timed: "ping baidu.com"
  12. User time (seconds): 0.00
  13. System time (seconds): 0.00
  14. Percent of CPU this job got: 0%
  15. Elapsed (wall clock) time (h:mm:ss or m:ss): 0:03.64
  16. Average shared text size (kbytes): 0
  17. Average unshared data size (kbytes): 0
  18. Average stack size (kbytes): 0
  19. Average total size (kbytes): 0
  20. Maximum resident set size (kbytes): 2140
  21. Average resident set size (kbytes): 0
  22. Major (requiring I/O) page faults: 0
  23. Minor (reclaiming a frame) page faults: 626
  24. Voluntary context switches: 10
  25. Involuntary context switches: 0
  26. Swaps: 0
  27. File system inputs: 0
  28. File system outputs: 0
  29. Socket messages sent: 0
  30. Socket messages received: 0
  31. Signals delivered: 0
  32. Page size (bytes): 4096
  33. Exit status: 0

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

4. 自定义输出格式

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

  1. C - Name and command line arguments used
  2. D - Average size of the process's unshared data area in kilobytes
  3. E - Elapsed time in a clock format
  4. F - Number of page faults
  5. I - Number of file system inputs by the process
  6. K - Average total memory use of the process in kilobytes
  7. M - Maximum resident set the size of the process during the lifetime in Kilobytes
  8. O - Number of file system outputs by the process
  9. P - Percentage of CPU that the job received
  10. R - Number of minor or recoverable page faults
  11. S - Total number of CPU seconds used by the system in kernel mode
  12. U - Total number of CPU seconds used by user mode
  13. W - Number of times the process was swapped out of main memory
  14. X - Average amount of shared text in the process
  15. Z - System's page size in kilobytes
  16. c - Number of times the process was context-switched
  17. e - Elapsed real time used by the process in seconds
  18. k - Number of signals delivered to the process
  19. p - Average unshared stack size of the process in kilobytes
  20. r - Number of socket messages received by the process
  21. s - Number of socket messages sent by the process
  22. t - Average resident set size of the process in kilobytes
  23. w - Number of time the process was context-switched voluntarily
  24. x - Exit status of the command

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

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

我们可以这样自定义:

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

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

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

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

  1. Elapsed Time = 0:03.92
  2. Inputs 0
  3. Outputs 0

原文来自:https://os.51cto.com/art/202004/613848.htm

本文地址:https://www.linuxprobe.com/linux-command-runtime-test.html编辑:冯瑞涛,审核员:逄增宝

Linux命令大全:https://www.linuxcool.com/

本文由博客一文多发平台 OpenWrite 发布!

原文链接:http://www.cnblogs.com/manongxianfeng/p/14320944.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号