/dev/null\". But soon I found it dis开发者_如何转开发plays program\'s output to stderr. I tried \"/usr/bin/time ./prog" />
开发者

How to ignore program's output when using /usr/bin/time?

开发者 https://www.devze.com 2023-01-22 00:35 出处:网络
I want to know how long a program running, so I tried \"/usr/bin/time ./program > /dev/null\". But soon I found it dis开发者_如何转开发plays program\'s output to stderr. I tried \"/usr/bin/time ./prog

I want to know how long a program running, so I tried "/usr/bin/time ./program > /dev/null". But soon I found it dis开发者_如何转开发plays program's output to stderr. I tried "/usr/bin/time ./program > /dev/null 2>&1" then, but /usr/bin/time's output not appear. So my question is, how to ignore program's output, and keep time's output.

Thanks a lot.


Use the -o flag to send the output from time to something else then stderr. For example, if you still want it on screen:

/usr/bin/time -o /dev/tty ./program >/dev/null 2>&1

Or, if you want output on stdout very badly:

/usr/bin/time sh -c './program >/dev/null 2>&1'

or similar. However, now you're also measuring the shell's time to start the process, which may or may not be a problem.


(Unable to comment)

It might be nicer to use a subshell:

/usr/bin/time ( ./program > /dev/null 2>&1 ) 2>&1

Or better yet a compound construct:

/usr/bin/time { ./program > /dev/null 2>&1; } 2>&1
0

精彩评论

暂无评论...
验证码 换一张
取 消