如何将 time 命令的输出重定向到 Linux 中的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13356628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:46:41  来源:igfitidea点击:

How to redirect the output of the time command to a file in Linux?

linuxbashtime

提问by ed82

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

关于 Linux 上的计时程序的一个小问题:time 命令允许测量程序的执行时间:

[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.

我知道还有其他时间实现选项 -o 来写入文件,但我的问题是关于没有这些选项的命令。

Any suggestions ?

有什么建议 ?

采纳答案by January

Try

尝试

{ time sleep 1 ; } 2> time.txt

which combines the STDERR of "time" and your command into time.txt

它将“时间”的 STDERR 和您的命令组合到 time.txt 中

Or use

或使用

{ time sleep 1 2> sleep.stderr ; } 2> time.txt

which puts STDERR from "sleep" into the file "sleep.stderr" and only STDERR from "time" goes into "time.txt"

将“sleep”中的 STDERR 放入“sleep.stderr”文件中,只有“time”中的 STDERR 进入“time.txt”

回答by sampson-chen

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

Wraptime和您在一组括号中计时的命令。

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

例如,在下列时间ls和写入的结果ls和定时到的结果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

$ (time ls) > ls_results 2> time_results

回答by alinsoar

&>out time command >/dev/null

in your case

在你的情况下

&>out time sleep 1 >/dev/null

then

然后

cat out

回答by Tim Ludwinski

If you are using GNU time instead of the bash built-in, try

如果您使用的是 GNU 时间而不是内置的 bash,请尝试

time -o outfile command

(Note: GNU time formats a little differently than the bash built-in).

(注意:GNU 时间格式与内置的 bash 格式略有不同)。

回答by Mark

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

如果您关心命令的错误输出,您可以像这样将它们分开,同时仍然使用内置的 time 命令。

{ 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 stderris used for time).

正如您看到的命令的错误转到一个文件(因为stderr用于time)。

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

不幸的是,您不能将它发送到另一个句柄(例如3>&2),因为它在{...}

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

也就是说,如果您可以使用 GNU 时间,请按照@Tim Ludwinski 所说的去做。

\time -o time.log command

回答by cmaster - reinstate monica

Simple. The GNU timeutility has an option for that.

简单的。GNUtime实用程序为此提供了一个选项。

But you have to ensure that you are notusing your shell's builtin timecommand, at least the bashbuiltin does not provide that option! That's why you need to give the full path of the timeutility:

但是您必须确保您没有使用 shell 的内置time命令,至少内置命令bash没有提供该选项!这就是为什么您需要提供time实用程序的完整路径:

/usr/bin/time -o time.txt sleep 1

回答by Adam Heath

#!/bin/bash

set -e

_onexit() {
    [[ $TMPD ]] && rm -rf "$TMPD"
}

TMPD="$(mktemp -d)"
trap _onexit EXIT

_time_2() {
    "$@" 2>&3
}

_time_1() {
    time _time_2 "$@"
}

_time() {
    declare time_label=""
    shift
    exec 3>&2
    _time_1 "$@" 2>"$TMPD/timing.$time_label"
    echo "time[$time_label]"
    cat "$TMPD/timing.$time_label"
}

_time a _do_something
_time b _do_another_thing
_time c _finish_up

This has the benefit of not spawning sub shells, and the final pipeline has it's stderr restored to the real stderr.

这具有不产生子外壳的好处,并且最终管道将其 stderr 恢复为真正的 stderr。

回答by jaycee xu

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

由于'time'命令的输出是错误输出,将其重定向为标准输出会更直观地进行进一步处理。

{ time sleep 1; } 2>&1 |  cat > time.txt

回答by Tobias G?dderz

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

如果您不想触及原始进程的 stdout 和 stderr,您可以将 stderr 重定向到文件描述符 3 并返回:

$ { 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:

您可以将其用于包装器(例如用于 cronjobs)来监视运行时:

#!/bin/bash

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

{ time { "$@" 2>&3; }; } 3>&2 2>> /my/runtime.log

回答by Locane

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
  • 其中“a”是附加的
  • 其中“o”后面是要附加到的文件名
  • 其中“f”是具有类似 printf 语法的格式
  • 其中 "%E" 产生 0:00:00; 时:分:秒
  • 我不得不调用 /usr/bin/time 因为bash“时间”正在践踏它并且没有相同的选项
  • 我只是想将输出输出到文件,与 OP 不同