Linux操作系统(击):重定向所有输出到文件 [英] Linux (Bash): Redirect all output to file

查看:853
本文介绍了Linux操作系统(击):重定向所有输出到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个命令行程序的所有输出重定向到一个文件中。我使用bash。某些输出被引导到一个文件,但仍有部分出现在终端和没有被存储到文件中。

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.

相似的症状说明如下:

重定向所有的输出到文件

不过,我已经尝试了建议的解决方案(捕获标准错误)都没有成功:

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

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

被创建的文件的stderr.txt但是是空的。

The file stderr.txt is created but is empty.

一个可能的线索是,命令行程序是一个客户端在同一台机器上的服务器进行通信。这可能是某些输出从服务器到来。

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?

编辑:

我已经证实,由服务器生成的输出减少。在单独的终端运行命令将导致两个端子输出部分,我可以通过管道从命令终端到一个文件输出所有。这就提出了有关如何捕获服务器输出的问题,但是这是一个不同的问题。

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.

推荐答案

如果启动服务器在同一终端上,那么它的服务器的标准错误是presumably被写入到终端,哪些是你不捕捉。

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.

要捕捉一切最好的方法是运行:

The best way to capture everything would be to run:

script output.txt

之前启动服务器或客户端。这将启动一个新的外壳,重定向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.

当你完成后,键入exit退出由脚本运行shell 命令。

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

这篇关于Linux操作系统(击):重定向所有输出到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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