Linux输出命令执行时间
- 安装moreutils工具
sudo apt install moreutils
- 使用moreutils工具中的ts命令将终端中的标准输入打上时间戳
长ping某个ip并将ping命令的结果加上时间戳以记录问题发生的时间点
➞ ping baidu.com |ts
5月 11 23:30:53 PING baidu.com (220.181.38.148) 56(84) bytes of data.
5月 11 23:30:53 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=45 time=90.5 ms
5月 11 23:30:54 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=45 time=73.10 ms
5月 11 23:30:55 64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=45 time=68.1 ms
结合tee命令将ping命令的结果保存到文件中,该命令将在终端中的标准输出同步写入到ping.log文件中,实现终端中的显示实时写入到文件中
➞ ping baidu.com 2>&1 |ts 2>&1 |tee ping.log
5月 11 23:33:00 PING baidu.com (39.156.69.79) 56(84) bytes of data.
5月 11 23:33:00 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=49 time=72.8 ms
5月 11 23:33:01 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=49 time=71.8 ms
5月 11 23:33:02 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=49 time=61.5 ms
查看ping.log文件内容
➞ cat ping.log
5月 11 23:33:00 PING baidu.com (39.156.69.79) 56(84) bytes of data.
5月 11 23:33:00 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=49 time=72.8 ms
5月 11 23:33:01 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=49 time=71.8 ms
5月 11 23:33:02 64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=49 time=61.5 ms