Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output redirection doesn't work for a certain program

On Linux, I'm trying to redirect stdout from a console application to a file instead of console. I do not have the source code. I tried several methods but all resulted in an empty file. Without output redirection everything works fine (I see console messages).

I tried, for example:

progname > out.txt
progname > out.txt 2&>1

And nothing appears in out.txt and in the console.

I tried to run the application with strace. When I do not use redirection, I see lines such as -

write(1, "bla bla", 8)

When I introduce output redirection, there are no write calls at all, which makes me think the application is testing for something before writing the message. What is the application looking for? How can I bypass it?

I'm using CentOS 5.5 and Bash.

like image 397
kshahar Avatar asked Dec 16 '10 09:12

kshahar


People also ask

How do I redirect program output to a file?

Option One: Redirect Output to a File Only To use bash redirection, you run a command, specify the > or >> operator, and then provide the path of a file you want the output redirected to. > redirects the output of a command to a file, replacing the existing contents of the file.

Which operator is used to redirect output of a program?

Regular output append >> operator This allows you to redirect the output from multiple commands to a single file. For example, I could redirect the output of date by using the > operator and then redirect hostname and uname -r to the specifications. txt file by using >> operator.

How do I redirect the output of an already running process?

The way we can redirect the output is by closing the current file descriptor and then reopening it, pointing to the new output. We'll do this using the open and dup2 functions. There are two default outputs in Unix systems, stdout and stderr. stdout is associated with file descriptor 1 and stderr to 2.


2 Answers

progname > out.txt 2>&1

Edit: If you actually did this and it was a typo in your question. It might be possible that the application checks if the output is a TTY.

Edit2: Ok, in that case, try script -qc progname > out.txt > 2>&1

In Debian/Ubuntu, script is a part of the bsdutils package which is an Essential package and always installed.

like image 103
plundra Avatar answered Oct 28 '22 08:10

plundra


Based on your strace it sounds like the program is testing if stdin and/our stdout is a tty. I've never used it but you could try using empty.sourceforge.net. It's suppose to create a pseudo-tty which should fool your program's check.

like image 24
Laurence Gonsalves Avatar answered Oct 28 '22 06:10

Laurence Gonsalves