Sandro
Sandro

Reputation: 2259

Program output that is neither STDOUT or STDERR?

I'm getting some really strange behavior when trying to capture the output of django's manager.py

> ./manage.py runserver 0.0.0.0:2869
Validating models...

0 errors found
Django version 1.3.1, using settings 'polling.settings'
Development server is running at http://0.0.0.0:2869/
Quit the server with CONTROL-C.
Error: That port is already in use.

> ./manage.py runserver 0.0.0.0:2869 >stdout 2>stderr

> cat stderr
Error: That port is already in use.

> cat stdout

>

Why am I getting the empty string when I try to capture the output on the second run?

Upvotes: 1

Views: 867

Answers (1)

praetorian droid
praetorian droid

Reputation: 3029

Any program could detect if its STDOUT and/or STDERR connected to terminal or not: man isatty(3). Python also has such a functionality: sys.stdout.isatty().

Probably your python script or its logging subsystem prints the lines that missing in the second run only when running on terminal i.e. in interactive mode. It's a common and proper practice. For example, there is no sense to print "Quit the server with CONTROL-C" if the output is redirected to a log file.

Below is a simple illustration:

#!/usr/bin/python

import sys

print "Print always."
if sys.stdout.isatty():
  print "Print only on tty!"

Here we go:

$ ./test.py 
Print always.
Print only on tty!
$ ./test.py > log
$ cat log
Print always.
$

Upvotes: 2

Related Questions