Crontab Fails to Write Logs

Crontab Fails to Write Logs

I'm using crontab to run a python script every minute. This is my how my crontab file looks like:

*/1 * * * *  source /root/.bashrc && source /home/env/bin/activate && python /home/manage.py shell < /home/script.py  >> /var/log/navjob.log 2>&1

When try to check cron output in syslog with this command #grep CRON /var/log/syslog the output is like this:

...CRON[764888]: (root) CMD (source /root/.bashrc && source /home/env/bin/activate && python /home/manage.py shell < /home/script.py  >> /var/log/navjob.log 2>&1)
...CRON[764887]: (CRON) info (No MTA installed, discarding output)

But the log file (/var/log/navjob.log) is empty and the code is not run!

1 Answer

When you chain commands and add redirection like

cmd1 && cmd2 && cmd3 >> somefile 2>&1

(or cmd1 ; cmd2 ; cmd3 >> somefile 2>&1 etc.) the redirections only apply to the last command in the chain. To redirect all of them, you need to group the commands like

{ cmd1 && cmd2 && cmd3 ; } >> somefile 2>&1

or (with a subshell)

( cmd1 && cmd2 && cmd3 ) >> somefile 2>&1

In this case, your cron job is likely failing at the very first source command, since source is a bashism and cron's default shell is /bin/sh - either add SHELL=/bin/bash before the crontab entry, or change source to the POSIX .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.