本文共 8915 字,大约阅读时间需要 29 分钟。
我想将一个进程的stdout和stderr都重定向到一个文件。 我该如何在中做到这一点?
对于tcsh,我必须使用以下命令:
command >& file
如果使用command &> file
,它将给出“无效的空命令”错误。
# Close STDOUT file descriptorexec 1<&-# Close STDERR FDexec 2<&-# Open STDOUT as $LOG_FILE file for read and write.exec 1<>$LOG_FILE# STDERR to STDOUTexec 2>&1echo "This line will appear in $LOG_FILE, not 'on screen'"
现在,简单的回显将写入$ LOG_FILE。 对守护有用。
对于原始帖子的作者,
这取决于您需要实现的目标。 如果您只需要重定向从脚本中调用的命令,就可以给出答案。 我的是关于在当前脚本中进行重定向,这会影响上述代码段之后的所有命令/内置命令(包括forks)。
另一个很酷的解决方案是立即重定向到std-err / out并同时重定向到记录器或日志文件,这涉及将“流”分成两部分。 此功能由'tee'命令提供,该命令可以一次写入/附加到多个文件描述符(文件,套接字,管道等):tee FILE1 FILE2 ...>(cmd1)>(cmd2)...
exec 3>&1 4>&2 1> >(tee >(logger -i -t 'my_script_tag') >&3) 2> >(tee >(logger -i -t 'my_script_tag') >&4)trap 'cleanup' INT QUIT TERM EXITget_pids_of_ppid() { local ppid="$1" RETVAL='' local pids=`ps x -o pid,ppid | awk "\\$2 == \\"$ppid\\" { print \\$1 }"` RETVAL="$pids"}# Needed to kill processes running in backgroundcleanup() { local current_pid element local pids=( "$$" ) running_pids=("${pids[@]}") while :; do current_pid="${running_pids[0]}" [ -z "$current_pid" ] && break running_pids=("${running_pids[@]:1}") get_pids_of_ppid $current_pid local new_pids="$RETVAL" [ -z "$new_pids" ] && continue for element in $new_pids; do running_pids+=("$element") pids=("$element" "${pids[@]}") done done kill ${pids[@]} 2>/dev/null}
因此,从一开始。 假设已将终端连接到/ dev / stdout(FD#1)和/ dev / stderr(FD#2)。 实际上,它可以是管道,插座或其他任何东西。
运行具有以上一行以及另外一行的脚本的结果:
echo "Will end up in STDOUT(terminal) and /var/log/messages"
...如下:
$ ./my_scriptWill end up in STDOUT(terminal) and /var/log/messages$ tail -n1 /var/log/messagesSep 23 15:54:03 wks056 my_script_tag[11644]: Will end up in STDOUT(terminal) and /var/log/messages
如果要查看更清晰的图片,请将以下两行添加到脚本中:
ls -l /proc/self/fd/ps xf
“最简单”的方式(仅限bash4): ls * 2>&- 1>&-
。
我想要一个解决方案,将stdout和stderr的输出写入日志文件,并且stderr仍在控制台上。 所以我需要通过tee复制stderr输出。
这是我发现的解决方案:
command 3>&1 1>&2 2>&3 1>>logfile | tee -a logfile
简短答案: Command >filename 2>&1
或Command &>filename
说明:
考虑以下代码,该代码将单词“ stdout”打印到stdout,将单词“ stderror”打印到stderror。
$ (echo "stdout"; echo "stderror" >&2)stdoutstderror
请注意,“&”运算符告诉bash 2是文件描述符(指向stderr),而不是文件名。 如果我们省略了“&”,此命令会将stdout
打印到stdout,并创建一个名为“ 2”的文件,并在其中写入stderror
。
通过试验以上代码,您可以亲自了解重定向运算符的工作方式。 例如,通过更改两个描述符1,2
哪个描述符被重定向到/dev/null
,以下两行代码分别删除了stdout中的所有内容和stderror中的所有内容(打印剩下的内容)。
$ (echo "stdout"; echo "stderror" >&2) 1>/dev/nullstderror$ (echo "stdout"; echo "stderror" >&2) 2>/dev/nullstdout
现在,我们可以解释为什么以下代码不产生任何输出的解决方案:
(echo "stdout"; echo "stderror" >&2) >/dev/null 2>&1
为了真正理解这一点,我强烈建议您阅读此 。 假设您已完成阅读,我们可以继续进行。 请注意,Bash从左到右处理。 因此,Bash首先看到>/dev/null
(与1>/dev/null
),并将文件描述符1设置为指向/ dev / null而不是stdout。 完成此操作后,Bash然后向右移动并看到2>&1
。 这会将文件描述符2设置为指向与文件描述符1 相同的文件 (而不是文件描述符1本身!!!!)(请参阅获取更多信息)。 由于文件描述符1指向/ dev / null,文件描述符2指向与文件描述符1相同的文件,因此文件描述符2现在也指向/ dev / null。 因此,两个文件描述符都指向/ dev / null,这就是为什么不呈现任何输出的原因。
要测试您是否真的理解这个概念,请尝试在切换重定向顺序时猜测输出:
(echo "stdout"; echo "stderror" >&2) 2>&1 >/dev/null
标准错误
这里的理由是Bash从左到右进行评估,看到2>&1,因此将文件描述符2设置为指向与文件描述符1相同的位置,即stdout。 然后,它将文件描述符1(记住> / dev / null = 1> / dev / null)设置为指向> / dev / null,从而删除通常发送到标准输出的所有内容。 因此,剩下的就是子shell中没有发送到stdout的内容(括号中的代码),即“ stderror”。 需要注意的有趣的是,即使1只是指向stdout的指针,通过2>&1
将指针2重定向到2>&1
也不构成指针2-> 1-> stdout的链。 如果这样做,由于将1重定向到/ dev / null,代码2>&1 >/dev/null
将给出指针链2-> 1-> / dev / null,因此该代码将不生成任何内容,与我们上面看到的相反。
最后,我注意到有一种更简单的方法可以做到这一点:
从3.6.4节 ,我们可以使用运算符&>
重定向stdout和stderr。 因此,要将任何命令的stderr和stdout输出都重定向到\\dev\\null
(这将删除输出),我们只需键入$ command &> /dev/null
或在我的示例中:
$ (echo "stdout"; echo "stderror" >&2) &>/dev/null
关键要点:
2>&1 >/dev/null
是!= >/dev/null 2>&1
。 一个产生输出,另一个不产生! 最后看看这些出色的资源:
,
以下功能可用于自动切换beetwen stdout / stderr和日志文件的输出过程。
#!/bin/bash #set -x # global vars OUTPUTS_REDIRECTED="false" LOGFILE=/dev/stdout # "private" function used by redirect_outputs_to_logfile() function save_standard_outputs { if [ "$OUTPUTS_REDIRECTED" == "true" ]; then echo "[ERROR]: ${FUNCNAME[0]}: Cannot save standard outputs because they have been redirected before" exit 1; fi exec 3>&1 exec 4>&2 trap restore_standard_outputs EXIT } # Params: $1 => logfile to write to function redirect_outputs_to_logfile { if [ "$OUTPUTS_REDIRECTED" == "true" ]; then echo "[ERROR]: ${FUNCNAME[0]}: Cannot redirect standard outputs because they have been redirected before" exit 1; fi LOGFILE=$1 if [ -z "$LOGFILE" ]; then echo "[ERROR]: ${FUNCNAME[0]}: logfile empty [$LOGFILE]" fi if [ ! -f $LOGFILE ]; then touch $LOGFILE fi if [ ! -f $LOGFILE ]; then echo "[ERROR]: ${FUNCNAME[0]}: creating logfile [$LOGFILE]" exit 1 fi save_standard_outputs exec 1>>${LOGFILE%.log}.log exec 2>&1 OUTPUTS_REDIRECTED="true" } # "private" function used by save_standard_outputs() function restore_standard_outputs { if [ "$OUTPUTS_REDIRECTED" == "false" ]; then echo "[ERROR]: ${FUNCNAME[0]}: Cannot restore standard outputs because they have NOT been redirected" exit 1; fi exec 1>&- #closes FD 1 (logfile) exec 2>&- #closes FD 2 (logfile) exec 2>&4 #restore stderr exec 1>&3 #restore stdout OUTPUTS_REDIRECTED="false" }
脚本内部用法示例:
echo "this goes to stdout"redirect_outputs_to_logfile /tmp/one.logecho "this goes to logfile"restore_standard_outputs echo "this goes to stdout"
@ fernando-fabreti
除了您所做的之外,我还对功能做了些微改动,并删除了&-结束符,它对我来说很有效。
function saveStandardOutputs { if [ "$OUTPUTS_REDIRECTED" == "false" ]; then exec 3>&1 exec 4>&2 trap restoreStandardOutputs EXIT else echo "[ERROR]: ${FUNCNAME[0]}: Cannot save standard outputs because they have been redirected before" exit 1; fi } # Params: $1 => logfile to write to function redirectOutputsToLogfile { if [ "$OUTPUTS_REDIRECTED" == "false" ]; then LOGFILE=$1 if [ -z "$LOGFILE" ]; then echo "[ERROR]: ${FUNCNAME[0]}: logfile empty [$LOGFILE]" fi if [ ! -f $LOGFILE ]; then touch $LOGFILE fi if [ ! -f $LOGFILE ]; then echo "[ERROR]: ${FUNCNAME[0]}: creating logfile [$LOGFILE]" exit 1 fi saveStandardOutputs exec 1>>${LOGFILE} exec 2>&1 OUTPUTS_REDIRECTED="true" else echo "[ERROR]: ${FUNCNAME[0]}: Cannot redirect standard outputs because they have been redirected before" exit 1; fi } function restoreStandardOutputs { if [ "$OUTPUTS_REDIRECTED" == "true" ]; then exec 1>&3 #restore stdout exec 2>&4 #restore stderr OUTPUTS_REDIRECTED="false" fi } LOGFILE_NAME="tmp/one.log" OUTPUTS_REDIRECTED="false" echo "this goes to stdout" redirectOutputsToLogfile $LOGFILE_NAME echo "this goes to logfile" echo "${LOGFILE_NAME}" restoreStandardOutputs echo "After restore this goes to stdout"
对于这种情况,当需要“管道”时,可以使用:
|&
例如:
echo -ne "15\n100\n"|sort -c |& tee >sort_result.txt
要么
TIMEFORMAT=%R;for i in `seq 1 20` ; do time kubectl get pods |grep node >>js.log ; done |& sort -h
这种基于bash的解决方案可以分别传递STDOUT和STDERR(从“ sort -c”的STDERR或从STDERR到“ sort -h”的管道)。
在您考虑使用exec 2>&1
类的情况下,我发现如果可能的话,使用如下bash函数重写代码会更容易阅读:
function myfunc(){ [...]}myfunc &>mylog.log
do_something 2>&1 | tee -a some_file
这将把stderr重定向到stdout,并将stdout重定向到some_file
并将其打印到stdout。
bash your_script.sh 1>file.log 2>&1
1>file.log
指示shell将STDOUT发送到文件file.log
, 2>&1
告诉它将STDERR(文件描述符2)重定向到STDOUT(文件描述符1)。
注意:顺序很重要,如liw.fi所指出, 2>&1 1>file.log
file.log不起作用。
您可以将stderr重定向到stdout,然后将stdout重定向到文件中:
some_command >file.log 2>&1
参见
该格式比仅在bash中起作用的最受欢迎的&>格式更受青睐。 在Bourne Shell中,它可以解释为在后台运行命令。 同样,格式更易读2(是STDERR)重定向到1(STDOUT)。
编辑:更改顺序,如注释中指出
看看。 应该:
yourcommand &>filename
(将stdout
和stderr
都重定向到文件名)。
LOG_FACILITY="local7.notice"LOG_TOPIC="my-prog-name"LOG_TOPIC_OUT="$LOG_TOPIC-out[$$]"LOG_TOPIC_ERR="$LOG_TOPIC-err[$$]"exec 3>&1 > >(tee -a /dev/fd/3 | logger -p "$LOG_FACILITY" -t "$LOG_TOPIC_OUT" )exec 2> >(logger -p "$LOG_FACILITY" -t "$LOG_TOPIC_ERR" )
与之相关:将stdOut&stderr写入syslog。
它几乎可以工作,但不是从信德()
奇怪的是,这有效:
yourcommand &> filename
但这给出了语法错误:
yourcommand &>> filenamesyntax error near unexpected token `>'
您必须使用:
yourcommand 1>> filename 2>&1
转载地址:http://jeogj.baihongyu.com/