如何将输出重定向到 /dev/null

发布于 2024-08-26 12:35:56 字数 408 浏览 11 评论 0原文

我有一个运行 a 命令的应用程序,如下所示:

; <开关> >& /dev/null

我可以配置 ,但我无法控制 。此命令生成的所有输出都将转到 /dev/null。我希望输出在屏幕上可见或重定向到日志文件。

我尝试使用 freopen() 和相关函数将 /dev/null 重新打开到另一个文件,但无法使其正常工作。

您还有其他想法吗?这有可能吗?

感谢您抽出时间。

PS:我正在Linux上工作。

I have an application that runs the a command as below:

<command> <switches> >& /dev/null

I can configure <command>, but I have no control over <switches> . All the output generated by this command goes to /dev/null. I want the output to be visible on screen or redirected to a log file.

I tried to use freopen() and related functions to reopen /dev/null to another file, but could not get it working.

Do you have any other ideas? Is this possible at all?

Thanks for your time.

PS: I am working on Linux.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

怪异←思 2024-09-02 12:35:56

可怕的黑客:

在二进制模式下使用文本编辑器打开应用程序,找到“/dev/null/”并将其替换为相同长度的字符串

e.g '~/tmp/log'
  • 首先进行备份
  • 要小心,
  • 要非常小心
  • 我是否提到了备份?

Terrible Hack:

use a text editor in binary mode open the app, find '/dev/null/' and replace it with a string of the same length

e.g '~/tmp/log'
  • make a backup first
  • be carefull
  • be very carefull
  • did I mention the backup?
半葬歌 2024-09-02 12:35:56

由于您可以修改运行的命令,因此可以使用简单的 shell 脚本作为包装器将输出重定向到文件。

#!/bin/bash
"$@" >> logfile

如果将其保存在路径中作为 capture_output.sh,则可以将 capture_output.sh 添加到命令的开头,以将程序的输出附加到日志文件。

Since you can modify the command you run you can use a simple shell script as a wrapper to redirect the output to a file.

#!/bin/bash
"$@" >> logfile

If you save this in your path as capture_output.sh then you can add capture_output.sh to the start of your command to append the output of your program to logfile.

梦在夏天 2024-09-02 12:35:56

在命令末尾附加 #,使其变为 #>& /dev/null,从而注释掉不需要的部分。

Append # at the end of your command so it becomes <command> # >& /dev/null, thus commenting out the undesired part.

百思不得你姐 2024-09-02 12:35:56

您的应用程序可能正在运行 shell 并向其传递该命令行。

您需要让它运行您编写的脚本。该脚本将用 >>/your/log 替换命令行中的 >/dev/null ,并使用修改后的命令行调用真实的 shell。

第一步是更改应用程序使用的 shell。更改环境变量 SHELL 就足够了,即运行您的应用程序。

SHELL=/home/user/bin/myshell theApp

如果这不起作用,请尝试暂时将 /bin/sh 链接到您的脚本。

myshell 将调用原始 shell,但在模式替换参数之后:

#!/bin/bash
sh ${1+"${@/\>\/dev\/null/>>\/your\/log}"}

按照这些思路应该可以工作。

Your application is probably running a shell and passing it that command line.

You need to make it run a script written by you. That script will replace >/dev/null in the command line with >>/your/log and call the real shell with the modified command line.

The first step is to change the shell used by the application. Changing the environment variable SHELL should suffice, i.e., run your application as

SHELL=/home/user/bin/myshell theApp

If that doesn't work, try momentarily linking /bin/sh to your script.

myshell will call the original shell, but after pattern-replacing the parameters:

#!/bin/bash
sh ${1+"${@/\>\/dev\/null/>>\/your\/log}"}

Something along these lines should work.

小女人ら 2024-09-02 12:35:56

您可以使用 gdb 对已运行的进程执行此操作。请参阅以下页面:http: //etbe.coker.com.au/2008/02/27/redirecting-output-from-a-running-process/

You can do this with an already running process by using gdb. See the following page: http://etbe.coker.com.au/2008/02/27/redirecting-output-from-a-running-process/

彡翼 2024-09-02 12:35:56

您能为该命令创建一个别名吗?如果是这样,请将其别名为另一个将输出转储到文件的命令。

Can you create an alias for that command? If so, alias it to another command that dumps output to a file.

魔法少女 2024-09-02 12:35:56

设备文件 /dev/tty 引用应用程序的控制终端 - 如果没有改变,那么这应该可以工作:

freopen("/dev/tty", "w", stdout);
freopen("/dev/tty", "w", stderr);

或者,您可以重新打开它们以指向日志文件:

freopen("/var/log/myapp.log", "a", stdout);
freopen("/var/log/myapp.err", "a", stderr);

The device file /dev/tty references your application's controlling terminal - if that hasn't changed, then this should work:

freopen("/dev/tty", "w", stdout);
freopen("/dev/tty", "w", stderr);

Alternatively, you can reopen them to point to a log file:

freopen("/var/log/myapp.log", "a", stdout);
freopen("/var/log/myapp.err", "a", stderr);
烟雨凡馨 2024-09-02 12:35:56

编辑:这不是一个好主意,当然不值得尝试,除非您知道这会破坏什么。它对我有用,也可能对你有用。

好吧,这是一个非常糟糕的黑客,可能不值得做。假设其他命令都不起作用,并且您根本无权访问二进制文件/应用程序(其中包含带有 /dev/null 的命令),并且您无法将输出重定向到其他文件(通过替换 /dev/null)。

然后,您可以删除 /dev/null ($> rm /dev/null) 并在其位置创建您自己的文件(最好使用软链接),所有数据都可以指向该文件。完成后,您可以使用以下命令再次创建 /dev/null:

$> mknod -m 666 /dev/null c 1 3

需要明确的是,这是一个糟糕的 hack,并且肯定需要 root 权限才能工作。您的重定向文件很有可能包含来自许多其他正在运行的应用程序/二进制文件的数据,并使用 /dev/null 作为接收器。

EDIT: This is NOT a good idea and certainly not worth trying unless you know what this can break. It works for me, may work for you as well.

Ok, This is a really bad hack and probably not worth doing. Assuming that none of the other commands works, and you simply do not have access to the binary/application (which contains the command with /dev/null) and you cannot re-direct the output to other file (by replacing /dev/null).

Then, you can delete /dev/null ($> rm /dev/null) and create your own file at its place (preferably with a soft link) where all the data can be directed. When you are done, you can create the /dev/null once again using following command:

$> mknod -m 666 /dev/null c 1 3

Just to be very clear, this is a bad hack and certainly requires root permissions to work. High chances that your re-directed file may contain data from many other applications/binaries which are running and use /dev/null as sink.

左秋 2024-09-02 12:35:56

它可能不完全重定向,但它允许在发送的任何地方获取输出

strace -ewrite -p $PID

这不是那么干净(显示类似: write(#,) 的行),但可以工作! (并且是单行:D)您可能也不喜欢参数被缩写的事实。要控制该设置,请使用 -s 参数来设置显示的字符串的最大长度。

它捕获所有流,因此您可能需要以某种方式对其进行过滤。

您可以过滤它:

strace -ewrite -p $PID 2>&1 | grep "write(1"

仅显示描述符 1 调用。2>&1 是将 stderr 重定向到 stdout,因为 strace 默认写入 stderr。

It may not exactly redirect, but it allows to get the output wherever it's being sent

strace -ewrite -p $PID

It's not that cleen (shows lines like: write(#,) ), but works! (and is single-line :D ) You might also dislike the fact, that arguments are abbreviated. To control that use -s parameter that sets the maxlength of strings displayed.

It catches all streams, so You might want to filter that somehow.

You can filter it:

strace -ewrite -p $PID 2>&1 | grep "write(1"

shows only descriptor 1 calls. 2>&1 is to redirect stderr to stdout, as strace writes to stderr by default.

ゃ懵逼小萝莉 2024-09-02 12:35:56

在 perl 中,如果您只想将 STDOUT 重定向到稍微有用的东西,您可以

open STDOUT, '>>', '/var/log/myscript.log';
open STDERR, '>>', '/var/log/myscript.err';

在脚本的开头执行类似以下操作,这将为脚本的其余部分重定向它。

In perl, if you just want to redirect STDOUT to something slightly more useful, you can just do something like:

open STDOUT, '>>', '/var/log/myscript.log';
open STDERR, '>>', '/var/log/myscript.err';

at the beginning of your script, and that'll redirect it for the rest of your script.

尐偏执 2024-09-02 12:35:56

按照 e-t172 的答案,您可以将最后一个开关设置为(或附加到它):

; echo 

Along the lines of e-t172's answer, can you set the last switch to (or append to it):

; echo 
人生戏 2024-09-02 12:35:56

如果您可以在将内容传递到 /dev/null 之前将某些内容内联(不确定您是否正在处理硬编码命令),则可以使用 tee 重定向到您选择的内容。

维基百科中允许升级命令的示例:

echo "Body of file..." | sudo tee root_owned_file > /dev/null

http://en.wikipedia.org/维基/Tee_(命令)

If you can put something inline before passing things to /dev/null (not sure if you are dealing with a hardcoded command), you could use tee to redirect to something of your choice.

Example from Wikipedia which allows escalation of a command:

echo "Body of file..." | sudo tee root_owned_file > /dev/null

http://en.wikipedia.org/wiki/Tee_(command)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文