Cron – No log file output (or email with output) running Python script via cron job

centos7cronlog-files

I have the following cron job running a Python script which seems to install okay with no issues after creating it in Crontab and saving (this is on a server running Centos7). I'm not seeing either a log file nor any output sent in to the email address included. I've tried this:

*/2 * * * * /home/local/DEV/mdub/FTWFB/FTWFBUploader.py > /home/local/DEV/mdub/FTWFB/logs`date +\%Y-\%m-\%d-\%H:\%M:\%S`-cron.log 2>&1 | mailx -s “Facebook Uploads - Cronlog" mdubs@gmail.com

and this:

*/2 * * * * /home/local/DEV/mdub/FTWFB/FTWFBUploader.py | tee /home/local/DEV/mdub/FTWFB/logs`date +\%Y-\%m-\%d-\%H:\%M:\%S`-cron.log | mailx -s “Facebook Uploads - Cronlog" mdubs@gmail.com

What am I doing wrong?

On both files I ran chmod +x (filename) and when I manually run the scripts they run and output as expected.

Best Answer

Firstly, when configuring a crontab use the full path for any commands and scripts you are calling. For example:

  • tee should be /usr/bin/tee
  • mailx should be /bin/mailx
  • date should be /bin/date

Note: If the paths are different on your system change as appropriate

Also, chaining commands in a crontab (i.e. piping, |) can get messy very quickly. It might be better to put those commands in a script and call that from cron instead.

If that doesn't help follow these general troubleshooting steps for cron:

Verify that crond is enabled and running, for example:

$ systemctl status crond | grep enabled
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled)

$ ps -ef | grep ^root.*crond
root      1251     1  0 May15 ?        00:00:55 /usr/sbin/crond -n

Check the cron logs to see if any errors show up in there (replace UserName with the name of the user):

grep UserName /var/log/cron

Check that user's mail file to see if any cron output is showing up there:

more /var/mail/UserName

If all else fails, append a redirect at the end of the crontab entry to help catch any spurious errors that might arise. For example instead of this:

* * * * * /bin/date | /usr/bin/mailx -s cron.test UserName@domain.tld

Do this:

* * * * * /bin/date | /usr/bin/mailx -s cron.test UserName@domain.tld > /tmp/crontab.test.UserName.log 2>&1