What could make `>` silently fail in Linux?

I ran this command:

python ./manage.py dumpdata partyapp.InvitationTemplate > partyapp_dump.json

To dump data into the partyapp_dump.json file. But all the data just gets printed on the screen and an empty partyapp_dump.json file is created.

Why could this happen? I tested ls > partyapp_dump.json and that worked perfectly.


Solution 1:

With > you only redirect the standard output. Try 2> instead to redirect the error output. Use &> to redirect both.

Solution 2:

Your python app must be writing it's output to the STDERR output channel instead of the normal STDOUT. Using the shell construct > only catches and redirects data written to the output channel, but there are actually several other channels that can be printed to, the most common being the second one, usually used for errors.

You can try trapping STDERR (2nd channel) as well like this:

python ./manage.py dumpdata partyapp.InvitationTemplate > partyapp_dump.json 2>&1

The 2>&1 construct connects the output stream for errors to normal output channel. It is unusual for a program to generate output that you would want to capture on the error channel; usually that would be reserved for debug information not application data. Please use this script with some caution since it is behaving in a non-standard way.

You could also dump the output and error channels to different files like this:

python ./manage.py dumpdata partyapp.InvitationTemplate > partyapp_dump.json 2> error_output.txt

Solution 3:

In addition to the already suggested stderr vs stdout output explanation, your application might simply ignore both of these streams and explicitly open "/dev/tty" for its output.