在 Linux 上使用 Bash 将所有输出重定向到文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16842014/
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 23:05:24  来源:igfitidea点击:

Redirect all output to file using Bash on Linux?

linuxbashstdoutstderr

提问by Stefan

I am trying to redirect all output from a command line programme to a file. I am using Bash. Some of the output is directed to a the file, but some still appears in the terminal and is not stored to the file.

我正在尝试将命令行程序的所有输出重定向到一个文件。我正在使用 Bash。一些输出被定向到文件,但有些仍然出现在终端中并且没有存储到文件中。

Similar symptoms are described here:

此处描述了类似的症状:

Redirect all output to file

将所有输出重定向到文件

However I have tried the proposed solution (capture stderr) without success:

但是,我尝试了建议的解决方案(捕获 stderr)但没有成功:

<cmd> <args> > stdout.txt 2> stderr.txt

The file stderr.txt is created but is empty.

文件 stderr.txt 已创建但为空。

A possible clue is that the command-line programme is a client communicating with a server on the same machine. It may be that some of the output is coming from the server.

一个可能的线索是命令行程序是与同一台机器上的服务器通信的客户端。某些输出可能来自服务器

Is there a way to capture all the output from the terminal, irrespective of its origin?

有没有办法捕获终端的所有输出,而不管其来源?

EDIT:

编辑:

I've confirmed that the missing output is generated by the server. Running the command in a separate terminal causes some output in both terminals, I can pipe all the output from the command terminal to a file. This raises issues about how to capture the server output, but that's a different question.

我已经确认缺少的输出是由服务器生成的。在单独的终端中运行命令会在两个终端中产生一些输出,我可以将命令终端的所有输出通过管道传输到一个文件。这引发了关于如何捕获服务器输出的问题,但这是一个不同的问题。

采纳答案by Brian Campbell

If the server is started on the same terminal, then it's the server's stderr that is presumably being written to the terminal and which you are not capturing.

如果服务器在同一终端上启动,那么服务器的 stderr 可能正在写入终端并且您没有捕获。

The best way to capture everything would be to run:

捕获所有内容的最佳方法是运行:

script output.txt

before starting up either the server or the client. This will launch a new shell with all terminal output redirected out output.txt as well as the terminal. Then start the server from within that new shell, and then the client. Everything that you see on the screen (both your input and the output of everything writing to the terminal from within that shell) will be written to the file.

在启动服务器或客户端之前。这将启动一个新的 shell,所有终端输出都重定向到 output.txt 以及终端。然后从新的 shell 中启动服务器,然后是客户端。您在屏幕上看到的所有内容(您的输入和从该 shell 写入终端的所有内容的输出)都将写入文件。

When you are done, type "exit" to exit the shell run by the scriptcommand.

完成后,输入“exit”退出script命令运行的shell 。

回答by developer

you can use this syntax to redirect all output stderr and stdout to stdout.txt

您可以使用此语法将所有输出 stderr 和 stdout 重定向到 stdout.txt

<cmd> <args> > allout.txt 2>&1 

回答by Morlock

回答by ThorSummoner

I had trouble with a crashing program *coughPHP cough* Upon crash the shell it was ran in reports the crash reason, Segmentation fault (core dumped)

我在程序崩溃时遇到了麻烦 *咳咳PHP* 崩溃时,它运行的 shell 报告了崩溃原因,Segmentation fault (core dumped)

To avoid this output not getting logged, the command can be run in a subshell that will capture and direct these kind of output:

为了避免此输出不被记录,该命令可以在一个子shell中运行,该子shell将捕获并引导这些类型的输出:

sh -c 'your_command' > your_stdout.log 2> your_stderr.err
# or
sh -c 'your_command' > your_stdout.log 2>&1

回答by chovy

You can execute a subshell and redirect all output while still putting the process in the background:

您可以执行子shell并重定向所有输出,同时仍将进程置于后台:

( ./script.sh blah > ~/log/blah.log 2>&1 ) &
echo $! > ~/pids/blah.pid

回答by Patrick Pijnappel

Though not POSIX, bash 4 has the &>operator:

虽然不是 POSIX,但 bash 4 有&>操作符:

command &> alloutput.txt

command &> alloutput.txt