带输出重定向的定时应用程序

timing application with output redirection

我正在 shell 中执行程序并重定向输出:

nice -10 appname > /tmp/output.log 2>&1

我还需要计时执行。 time 命令有效,但它的输出也与主应用程序的输出一起写入 output.log

time -p nice -10 appname > /tmp/output.log 2>&1

我尝试将应用程序括在括号中,但会引发错误:

time -p (nice -10 appname > /tmp/output.log 2>&1)
sh: 1: Syntax error: "(" unexpected

如何将 time 输出写入标准输出,同时仍将应用程序输出重定向到文件?

您可以使用假人 printf/echo:

time -p printf "$(nice -10 appname > /tmp/output.log 2>&1)"


或者这里的文档:

time -p sh <<EOF
nice -10 appname > /tmp/output.log 2>&1
EOF


或者使用 Shell 的 -c 选项:

time -p sh -c "nice -10 appname > /tmp/output.log 2>&1"


或者函数 (Bash):

nicefun(){
nice -10 appname > /tmp/output.log 2>&1
}
time -p nicefun


或专用脚本:

nice.sh:

#!/bin/sh
nice -10 appname > /tmp/output.log 2>&1

用法:

time -p path/to/nice.sh