Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect output away from /dev/null

Tags:

c

redirect

stdio

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.

like image 627
Gowtham Avatar asked Mar 24 '10 12:03

Gowtham


2 Answers

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?
like image 120
lexu Avatar answered Sep 22 '22 16:09

lexu


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.

like image 38
Ven'Tatsu Avatar answered Sep 19 '22 16:09

Ven'Tatsu