你如何知道一个正在运行的进程需要多长时间才能完成?
例子:
date; dd bs=1m if=/foo of=bar; date
Run Code Online (Sandbox Code Playgroud)
^这个例子只有 1 秒的分辨率。
任何外壳都是可以接受的。
les*_*ana 88
使用time:
$ time longrunningcommand --takeyourtime
Run Code Online (Sandbox Code Playgroud)
time将命令行的其余部分作为命令执行(在本例中longrunningcommand --takeyourtime),当命令完成时,它将打印经过的时间。
示例输出(bash 内置time)
$ time longrunningcommand --takeyourtime
...
output of longrunningcommand
...
real 0m5,020s
user 0m0,010s
sys 0m0,010s
Run Code Online (Sandbox Code Playgroud)
有趣的信息是real 0m5,020s。这意味着该命令大约需要 5 秒钟。有关其他数字的更多信息,请参见此处:https : //stackoverflow.com/questions/556405/what-do-real-user-and-sys-mean-in-the-output-of-time1
time是大多数 shell 中的内置命令。上面的示例输出来自 bash 内置。有时您会想要使用“系统时间”。例如为了避免暂时生成外壳。
如果你想使用系统,time请这样做:
$ /usr/bin/time longrunningcommand --getsomecoffee
$ \time longrunningcommand --callmom
$ command time longrunningcommand --mowthelawn
Run Code Online (Sandbox Code Playgroud)
反斜杠适用于 bash 和其他一些 shell。command适用于大多数 shell。/usr/bin/time应该总是工作。
使用系统的示例输出time:
$ /usr/bin/time ./longrunningcommand --callmom
... output of longrunningcommand
0.00user 0.01system 0:06.02elapsed 0%CPU (0avgtext+0avgdata 3656maxresident)k
0inputs+0outputs (0major+1089minor)pagefaults 0swaps
Run Code Online (Sandbox Code Playgroud)
有趣的信息是0:06.02elapsed。这意味着该命令大约需要 6 秒。有关其他数字的含义,请阅读以下手册页time:http : //man7.org/linux/man-pages/man1/time.1.html
你可以改变系统的输出time。用于-p获取类似于 shell builtin 的输出time。
$ /usr/bin/time -p sleep 0.5
real 0.50
user 0.00
sys 0.00
Run Code Online (Sandbox Code Playgroud)
用于-f编写您自己的格式。%E是“经过”的部分。那是您通常最感兴趣的部分。
$ /usr/bin/time -f %E sleep 0.5
0:00.50
Run Code Online (Sandbox Code Playgroud)
shell 内置命令和系统命令的区别不仅仅是输出的格式。有关更多信息,请阅读此处的区别:内置命令与非内置命令有什么区别?
如何重定向或捕获输出
为了演示观察命令hellostdoutstderr:
#!/bin/sh
sleep 0.5
echo stdout
echo stderr >&2
Run Code Online (Sandbox Code Playgroud)
示例调用:
$ ./hellostdoutstderr
stdout
stderr
Run Code Online (Sandbox Code Playgroud)
分别捕获 stdout 和 stderr
$ ./hellostdoutstderr >stdout 2>stderr
$ cat stdout
stdout
$ cat stderr
stderr
Run Code Online (Sandbox Code Playgroud)
系统time打印到 stderr,因此它在 stderr 重定向中被捕获
$ /usr/bin/time ./hellostdoutstderr >stdout 2>stderr
$ cat stdout
stdout
$ cat stderr
stderr
0.00user 0.00system 0:00.50elapsed 1%CPU (0avgtext+0avgdata 3672maxresident)k
0inputs+16outputs (0major+311minor)pagefaults 0swaps
Run Code Online (Sandbox Code Playgroud)
你可以告诉系统time打印到一个单独的文件
$ /usr/bin/time -o timeout ./hellostdoutstderr >stdout 2>stderr
$ cat stdout
stdout
$ cat stderr
stderr
$ cat timeout
0.00user 0.00system 0:00.50elapsed 1%CPU (0avgtext+0avgdata 3676maxresident)k
0inputs+16outputs (0major+309minor)pagefaults 0swaps
Run Code Online (Sandbox Code Playgroud)
time即使 stdout 和 stderr 被重定向,bash 内置函数也始终打印到终端。这是可能的,因为它是一个内置函数并且可以做任何它喜欢的事情(在 shell 中)
$ time ./hellostdoutstderr >stdout 2>stderr
real 0m0,511s
user 0m0,005s
sys 0m0,006s
Run Code Online (Sandbox Code Playgroud)
(stdout 和 stderr 被捕获,但time仍然可以打印到 shell)
仍然重定向这里输出阅读:https : //stackoverflow.com/questions/18348593/how-can-i-output-from-usr-bin-time-to-a-file-at-the-right-location-执行内
或在这里:https : //www.cyberciti.biz/faq/unix-linux-time-command-examples-usage-syntax/
计时更复杂的命令,你有几个选项
如果您只想一个接一个地计时两个命令
$ time { command1 ; command2 ; }
Run Code Online (Sandbox Code Playgroud)
也适用于管道
$ time { command1 | command2 ; }
Run Code Online (Sandbox Code Playgroud)
对于更复杂的东西
$ time sh -c ' complex command chain '
Run Code Online (Sandbox Code Playgroud)
但请注意引用和其他恶作剧。最好将命令放在脚本文件中:
$ time ./script.sh
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
72708 次 |
| 最近记录: |