How to redirect the output of the time command to

2019-01-03 23:03发布

Just a little question about timing programs on Linux: the time command allows to measure the execution time of a program:

[ed@lbox200 ~]$ time sleep 1

real    0m1.004s
user    0m0.000s
sys     0m0.004s

Which works fine. But if I try to redirect the output to a file, it fails.

[ed@lbox200 ~]$ time sleep 1 > time.txt

real    0m1.004s
user    0m0.001s
sys     0m0.004s

[ed@lbox200 ~]$ cat time.txt 
[ed@lbox200 ~]$ 

I know there are other implementations of time with the option -o to write a file but my question is about the command without those options.

Any suggestions ?

标签: linux bash time
11条回答
疯言疯语
2楼-- · 2019-01-03 23:10

Simple. The GNU time utility has an option for that.

But you have to ensure that you are not using your shell's builtin time command, at least the bash builtin does not provide that option! That's why you need to give the full path of the time utility:

/usr/bin/time -o time.txt sleep 1
查看更多
Luminary・发光体
3楼-- · 2019-01-03 23:13

If you care about the command's error output you can separate them like this while still using the built-in time command.

{ time your_command 2> command.err ; } 2> time.log

or

{ time your_command 2>1 ; } 2> time.log

As you see the command's errors go to a file (since stderr is used for time).

Unfortunately you can't send it to another handle (like 3>&2) since that will not exist anymore outside the {...}

That said, if you can use GNU time, just do what @Tim Ludwinski said.

\time -o time.log command
查看更多
Bombasti
4楼-- · 2019-01-03 23:13

Since the output of 'time' command is error output, redirect it as standard output would be more intuitive to do further processing.

{ time sleep 1; } 2>&1 |  cat > time.txt
查看更多
SAY GOODBYE
5楼-- · 2019-01-03 23:16

Wrap time and the command you are timing in a set of brackets.

For example, the following times ls and writes the result of ls and the results of the timing into outfile:

$ (time ls) > outfile 2>&1

Or, if you'd like to separate the output of the command from the captured output from time:

$ (time ls) > ls_results 2> time_results
查看更多
家丑人穷心不美
6楼-- · 2019-01-03 23:17

I ended up using:

/usr/bin/time -ao output_file.txt -f "Operation took: %E" echo lol
  • Where "a" is append
  • Where "o" is proceeded by the file name to append to
  • Where "f" is format with a printf-like syntax
  • Where "%E" produces 0:00:00; hours:minutes:seconds
  • I had to invoke /usr/bin/time because the bash "time" was trampling it and doesn't have the same options
  • I was just trying to get output to file, not the same thing as OP
查看更多
Lonely孤独者°
7楼-- · 2019-01-03 23:23

If you don't want to touch the original process' stdout and stderr, you can redirect stderr to file descriptor 3 and back:

$ { time { perl -le "print 'foo'; warn 'bar';" 2>&3; }; } 3>&2 2> time.out
foo
bar at -e line 1.
$ cat time.out

real    0m0.009s
user    0m0.004s
sys     0m0.000s

You could use that for a wrapper (e.g. for cronjobs) to monitor runtimes:

#!/bin/bash

echo "[$(date)]" "$@" >> /my/runtime.log

{ time { "$@" 2>&3; }; } 3>&2 2>> /my/runtime.log
查看更多
登录 后发表回答