开发者

How to redirect output away from /dev/null

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 g开发者_开发问答oes 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.


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?


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.


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


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.


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/


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


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);


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.


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.


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.


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

; echo 


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜