我知道如何重定向标准输出到一个文件:

exec > foo.log
echo test

这将把“test”放到foo.log文件中。

现在我想将输出重定向到日志文件中,并将其保持在stdout

也就是说,它可以在脚本之外简单地完成:

script | tee foo.log

但我想在脚本中声明它

我试着

exec | tee foo.log

但这并没有起作用。


当前回答

这两种方法都不是完美的解决方案,但你可以尝试以下几件事:

exec >foo.log
tail -f foo.log &
# rest of your script

or

PIPE=tmp.fifo
mkfifo $PIPE
exec >$PIPE
tee foo.log <$PIPE &
# rest of your script
rm $PIPE

如果你的脚本出了问题,第二种方法会留下一个管道文件,这可能是问题,也可能不是问题(例如,也许你可以随后在父shell中rm它)。

其他回答

这两种方法都不是完美的解决方案,但你可以尝试以下几件事:

exec >foo.log
tail -f foo.log &
# rest of your script

or

PIPE=tmp.fifo
mkfifo $PIPE
exec >$PIPE
tee foo.log <$PIPE &
# rest of your script
rm $PIPE

如果你的脚本出了问题,第二种方法会留下一个管道文件,这可能是问题,也可能不是问题(例如,也许你可以随后在父shell中rm它)。

接受的答案不保留STDERR作为单独的文件描述符。这意味着

./script.sh >/dev/null

会不会输出条到终端,只到日志文件,和

./script.sh 2>/dev/null

将输出foo和bar到终端。显然不是这样的 一个正常用户可能会期待的行为。这可以是 通过使用两个独立的三通过程都附加到相同的固定 日志文件:

#!/bin/bash

# See (and upvote) the comment by JamesThomasMoon1979 
# explaining the use of the -i option to tee.
exec >  >(tee -ia foo.log)
exec 2> >(tee -ia foo.log >&2)

echo "foo"
echo "bar" >&2

(请注意,上面的内容最初并不会截断日志文件——如果你想要这种行为,你应该添加

>foo.log

到脚本的顶部。)

The POSIX.1-2008 specification of tee(1) requires that output is unbuffered, i.e. not even line-buffered, so in this case it is possible that STDOUT and STDERR could end up on the same line of foo.log; however that could also happen on the terminal, so the log file will be a faithful reflection of what could be seen on the terminal, if not an exact mirror of it. If you want the STDOUT lines cleanly separated from the STDERR lines, consider using two log files, possibly with date stamp prefixes on each line to allow chronological reassembly later on.

使bash脚本日志转换为syslog的简单方法。脚本输出可以通过/var/log/syslog和stderr获得。Syslog将添加有用的元数据,包括时间戳。

在顶部添加这一行:

exec &> >(logger -t myscript -s)

或者,将日志发送到一个单独的文件:

exec &> >(ts |tee -a /tmp/myscript.output >&2 )

这需要更多的utils(用于ts命令,它添加时间戳)。

在你的脚本文件中,把所有的命令都放在括号里,就像这样:

(
echo start
ls -l
echo end
) | tee foo.log

busybox、macOS bash和非bash shell的解决方案

公认的答案当然是bash的最佳选择。我在Busybox环境中工作,没有访问bash,它不理解exec > >(tee log.txt)语法。它也没有正确地执行exec >$PIPE,试图创建一个与命名管道同名的普通文件,该文件失败并挂起。

希望这对那些没有bash的人有用。

同样,对于任何使用命名管道的人来说,rm $ pipe是安全的,因为这将断开管道与VFS的链接,但是使用它的进程仍然在它上维护引用计数,直到它们完成。

注意,使用$*并不一定安全。

#!/bin/sh

if [ "$SELF_LOGGING" != "1" ]
then
    # The parent process will enter this branch and set up logging

    # Create a named piped for logging the child's output
    PIPE=tmp.fifo
    mkfifo $PIPE

    # Launch the child process with stdout redirected to the named pipe
    SELF_LOGGING=1 sh $0 $* >$PIPE &

    # Save PID of child process
    PID=$!

    # Launch tee in a separate process
    tee logfile <$PIPE &

    # Unlink $PIPE because the parent process no longer needs it
    rm $PIPE    

    # Wait for child process, which is running the rest of this script
    wait $PID

    # Return the error code from the child process
    exit $?
fi

# The rest of the script goes here