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.
With > you only redirect the standard output. Try 2> instead to redirect the error output. Use &> to redirect both.
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:
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:
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.
If the
noclobber
bash option is set, then > redirection will fail (albeit not silently) if the destination file already exists.For better portability, use
cmd >| file
to force overwriting any existing file.If you are lost you can always try to run it with strace to see what processes are doing: