I have a simple cronjob running every day at 18:35:
05 18 * * * ~/job.sh 2>&1 >> ~/job.log
So the output of ~/job.sh should be written into ~/job.log. In job.sh, there are some echo commands and a few python scripts are executed, e.g.:
echo 'doing xyz'
python doXYZ.py
Now, whatever output the python scripts produce, they are not written into ~/job.log. I only see the echo text in ~/job.log. How can I redirect the complete output of the shell script to ~/job.log?
Arkaitz has the simplest solution. However, to see what’s wrong with your snippet we need to go into the bash manual:
So apparently the output redirection only redirects to the target of the other stream, not to the other stream itself. When bash parses your command line it will come upon the
2>&1clause and redirectstderrto the target ofstdout, which at this point is still the console (or whatever is attached tocron‘sstdout). Only after this willstdoutbe redirected.So what you really want is: