抖音资讯

douyinzx

linux查看运行时间命令(linux查看用户执行命令历史)

iseeyu2年前 (2024-05-07)抖音资讯113

良许在工作中,写过一个 Shell 脚本,这个脚本可以从 4 个 NTP 服务器轮流获取时间,然后将最可靠的时间设置为系统时间。

因为我们对于时间的要求比较高,需要在短时间内就获取到正确的时间。所以我们就需要对这个脚本运行时间进行测试,看看从开始运行到正确设置时间需要花费多少时间。

其实在工作中,还有很多情况下需要测试一个脚本或者程序运行多少时间,特别是对于时间性要求比较高的系统更是如此。

对于时间的测试,我们可以用到一个命令:time 。下面我们就详细看看如何使用 time 命令来对脚本/命令进行测时。

1. time 命令基本用法

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

linux查看运行时间命令(linux查看用户执行命令历史)

$ 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 命令在用户空间及内核空间所运行的时间。

2020 精选 阿里/腾讯等一线大厂 面试、简历、进阶、电子书 私信我「1024」免费获取

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

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

2020 精选 阿里/腾讯等一线大厂 面试、简历、进阶、电子书 私信我「1024」免费获取

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://0291.com.cn/post/43212.html

相关文章

抖音怎么开通商品橱窗功能(开通抖音橱窗的要求和流程)

抖音怎么开通商品橱窗功能(开通抖音橱窗的要求和流程)

抖音是一款非常流行的短视频应用程序,已成为许多人生活中不可或缺的一部分,许多用户在抖音上分享自己的生活、才艺和产品。 电商爆火的当下,也有不少人想知道如何在抖音上开设自己的橱窗,今天我们就来分享一下,抖音橱窗怎么开通。 开通橱窗的条件 在抖音上开通橱窗,需要满足以下条件: 粉丝数...

没钱没资源,穷人如何翻身?这几个小项目让你月入过万

没钱没资源,穷人如何翻身?这几个小项目让你月入过万

抖音的崛起让无数普通人逆袭了,实现了一定程度上的财富自由。那现在作为普通人,做什么才能在抖音赚到钱呢?今天我就分享3个成本低,利润高的抖音小项目,新手学起来非常快,执行力强的人,不超过3个月就能快速,月赚1万+都不是事。01 全民任务什么是全民任务?由于抖音坐拥6亿的日活量...

快手网站入口链接(快手网页版在线看入口)

快手网站入口链接(快手网页版在线看入口)

快手店铺报白在哪找找这个入口呢,在哪里可以申请报白名单呢?在快手这个平台上我们可以看到很多相关的商家服务,报白名单就是通过这些商家服务申请的。   报白入口 快手店铺报白在哪找入口呢?商家报白名单这个入口其实就在快手的官方网站里面,我们可以在网上搜索一下快手官网...

fla文件用什么软件打开(万能的打开工具介绍)

fla文件用什么软件打开(万能的打开工具介绍)

我们在用电脑的时候,一些文件想打开必须要相对应的软件安装好,才能打开这些文件。有些文件可能我们不是经常使用,只是查看一下,那安装这些文件对应的软件势必会占用电脑的资源。下面默默分享一款能够打开多种格式文件的工具Universal...

抖音关闭屏幕只听声音怎么设置(锁屏后继续听直播的操作)

抖音关闭屏幕只听声音怎么设置(锁屏后继续听直播的操作)

抖音现在很火,差不多成为了大家日常休息时使用最多的软件。每天没有事的时候总想打开抖音看看里面的视频休息一下。为了方便大家的使用每次打开软件都会直接播放视频并带有声音,但是在工作偷懒时,本来想偷偷摸鱼,如果抖音没有关声音被发现了就会很尴尬。 现在抖音可以设置启动抖音时默认静音了,这个功...

新营销少帅刘雷:抖音营销入门的8个正确动作

新营销少帅刘雷:抖音营销入门的8个正确动作

1、我以前就反复说过,未来的业有一半是娱乐业,抖音的玩法侧重于“娱乐属性”,娱乐化是首要理念。 2、玩抖音的第一步是精准定位自己的账号基调,细分你擅长的一个领域,比如:有趣、颜值、可爱、玄技、等,越垂直细化,越有自己的个性。越容易精准营销。 3、抖音打动人心的6个支点...

现在,非常期待与您的又一次邂逅

我们努力让每一部企业宣传片和抖音短视频成为商业大片