Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirection of stdout to a file not working

I have a script that uses subprocesses to fetch HTML:

misha@misha-K42Jr:~/git/domain_classifier$ python webkit_retrieve.py error-cut.txt html/error -N 5
http://kurabo.co.jp HostNotFoundError
http://monarch.com HostNotFoundError
http://nssmgmt.com HostNotFoundError
http://sbcglobal.net HostNotFoundError
http://dynamixcorp.com SslHandshakeFailedError
http://groupe-synox.com RemoteHostClosedError
QFont::setPixelSize: Pixel size <= 0 (0)
http://www.cnn.com NoError
http://pacbell.net TimeoutError

If I run the same script, but redirect output to a file, I get nothing in the output:

misha@misha-K42Jr:~/git/domain_classifier$ python webkit_retrieve.py error-cut.txt html/error -N 5 > stdout.txt
QFont::setPixelSize: Pixel size <= 0 (0)
misha@misha-K42Jr:~/git/domain_classifier$ cat stdout.txt
misha@misha-K42Jr:~/git/domain_classifier$

Why is the output empty? Should it not contain the same things that were printed to stdout in the first case?

The question is not about merge stdout and stderr but why redirected stdout produce an empty file

like image 392
mpenkov Avatar asked Nov 20 '12 03:11

mpenkov


2 Answers

use &> for redirection, this should redirect stdout and stderr to designated file

like image 131
Saddam Abu Ghaida Avatar answered Sep 30 '22 01:09

Saddam Abu Ghaida


You have sent stdout to the file, but your program is reporting errors which go to stderr. To setup redirection of stderr, use 2> syntax.

This link might help: http://www.tldp.org/LDP/abs/html/io-redirection.html

like image 20
mcalex Avatar answered Sep 30 '22 01:09

mcalex