写标准输出和放大器; STDERR到一个日志文件,也写STDERR筛选 [英] Write STDOUT & STDERR to a logfile, also write STDERR to screen

查看:102
本文介绍了写标准输出和放大器; STDERR到一个日志文件,也写STDERR筛选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一些命令,并捕获所有输出到日志文件。我也想打印任何错误到屏幕(或者任选邮寄输出到某人)。

下面是一个例子。以下命令将运行三个命令,将所有的输出(stdout和stderr)写入一个日志文件。

  {命令1&放大器;&安培;命令2&功放;&安培;命令3; }> LOGFILE.LOG 2  - ;&放大器; 1

下面是我想要与这些命令的输出做:


  • STDERR和STDOUT所有命令去一个日志文件,万一我需要它以后---我平时不会看这里,除非有问题。

  • 打印STDERR到屏幕(或可选,管道为/ bin /邮箱),以便任何错误脱颖而出,不被忽略。

  • 这将是很好,如果返回codeS仍然可以使用,这样我就可以做一些错误处理。也许我想,如果有发送电子邮件的错误,像这样的:

    {命令1&放大器;&安培;命令2&功放;&安培;命令3; }> LOGFILE.LOG 2>&安培; 1 || mailx的-s​​有错误stefanl@example.org


我遇到的问题是,在STDERR I / O重定向失去上下文。 A'2>&安培; 1'将STDERR转换成标准输出,因此,如果我做我2无法查看错误> error.log中

下面是几个例子多汁。让我们,我运行一些熟悉的构建命令pretend,但我不想让我用'--keep正在进行'标志整个构建停止,只是因为一次错误的。

  {方法:./configure&放大器;&安培;使--keep持续和放大器;&安培;使安装; }> build.log 2  - ;&放大器; 1

或者,这里有一个简单的(也许是稀松)建立和部署脚本,这将保持在一个错误的情况下去。

  {方法:./configure&放大器;&安培;使--keep持续和放大器;&安培;使安装和放大器;&安培; rsync的-av --keep正在进行/富devhost:/富}>建立和-deploy.log 2  - ;&放大器; 1

我想我想涉及到某种击I / O重定向,但我想不通这一点。


解决方案

 (./ DOIT>>日志)2  - ;&放大器; 1 |三通-a日志

这将输出和其追加到日志文件。

然后标准错误将会被转换到通过管道输送到 T恤其追加到日志(如果你有猛砸4,可以替换<$ C $标准输出C> 2 - ;&放大器; 1 | 与 |&安培; ),并将其发送到stdout,这将无论是在TTY出现或可通过管道输送到另一个命令。

我用追加模式两种,这样无论哪个命令外壳重定向和 T恤打开该文件,你会不会吹走原件。这就是说,它可能是可能的标准错误/标准输出以意想不到的方式交错。

I would like to run several commands, and capture all output to a logfile. I also want to print any errors to the screen (or optionally mail the output to someone).

Here's an example. The following command will run three commands, and will write all output (STDOUT and STDERR) into a single logfile.

{ command1 && command2 && command3 ; } > logfile.log 2>&1

Here is what I want to do with the output of these commands:

  • STDERR and STDOUT for all commands goes to a logfile, in case I need it later--- I usually won't look in here unless there are problems.
  • Print STDERR to the screen (or optionally, pipe to /bin/mail), so that any error stands out and doesn't get ignored.
  • It would be nice if the return codes were still usable, so that I could do some error handling. Maybe I want to send email if there was an error, like this:

    { command1 && command2 && command3 ; } > logfile.log 2>&1 || mailx -s "There was an error" stefanl@example.org

The problem I run into is that STDERR loses context during I/O redirection. A '2>&1' will convert STDERR into STDOUT, and therefore I cannot view errors if I do 2> error.log

Here are a couple juicier examples. Let's pretend that I am running some familiar build commands, but I don't want the entire build to stop just because of one error so I use the '--keep-going' flag.

{ ./configure && make --keep-going && make install ; } > build.log 2>&1

Or, here's a simple (And perhaps sloppy) build and deploy script, which will keep going in the event of an error.

{ ./configure && make --keep-going && make install && rsync -av --keep-going /foo devhost:/foo} > build-and-deploy.log 2>&1

I think what I want involves some sort of Bash I/O Redirection, but I can't figure this out.

解决方案

(./doit >> log) 2>&1 | tee -a log

This will take stdout and append it to log file.

The stderr will then get converted to stdout which is piped to tee which appends it to the log (if you are have Bash 4, you can replace 2>&1 | with |&) and sends it to stdout which will either appear on the tty or can be piped to another command.

I used append mode for both so that regardless of which order the shell redirection and tee open the file, you won't blow away the original. That said, it may be possible that stderr/stdout is interleaved in an unexpected way.

这篇关于写标准输出和放大器; STDERR到一个日志文件,也写STDERR筛选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆