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?
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:
Here we go: