time的结果输入到文件
同事在写一个脚本的时候,在将time获取的执行时间结果输出到文件时,发现无法将结果输入到文件。通过测试发现可以通过如下方法实现time 结果输入到文件:
1{ time sleep 1 ; } 2> time.txt
2{ time sleep 1 2> sleep.stderr ; } 2> time.txt
3(time ls) > outfile 2>&1
4(time ls) > ls_results 2> time_results
5/usr/bin/time -o time.txt sleep 1
Note: some shells (e.g., bash(1)) have a built-in time command that provides less functionality than the command described here. To access the real command, you may need to specify its pathname (something like /usr/bin/time).
需要注意的是,这里有两个time,其中一个是bash内置的time命令,一个是单独的time命令(位于/usr/bin/time位置),默认情况下使用的是bash内置的time命令,由于该命令默认是输出到标准错误的,所以需要将标准错误输出到文件才可以记录对应的时间值。
man time的gun选项(即/usr/bin/time)发现有如下参数
1OPTIONS
2-p When in the POSIX locale, use the precise traditional format
3 "real %f\nuser %f\nsys %f\n"
4-o FILE, --output=FILE
5 Do not send the results to stderr, but overwrite the specified file.
使用time命令输出的结果看起来非常不美观,如下:
1[root@361way ~]# /usr/bin/time -o time.txt sleep 1
2[root@361way ~]# more time.txt
30.00user 0.00system 0:01.00elapsed 0%CPU (0avgtext+0avgdata 2368maxresident)k
40inputs+0outputs (0major+182minor)pagefaults 0swaps
配合-p参数后,输出结果和bash内置time效果相同,如下:
1[root@361way ~]# /usr/bin/time -o time.txt -p sleep 1
2[root@361way ~]# cat time.txt
3real 1.00
4user 0.00
5sys 0.00
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/time-outputfile/5294.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.