1

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.

1

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
1

The best practice here is to write a script and call that script by full path within cron. This really is just reinforcing what Gene stated, but writing a script that will run those commands allows you the ability to test the script before placing it in a cron.

Additionally, this makes cron easier to read over the long run.

-1

Cron doesn't allow % characters.

  • Nah, with how the original poster has it written it will work. I just tried the following on a CentOS 7 system, worked fine: * * * * * /usr/bin/echo `/bin/date +\%Y-\%m-\%d-\%H:\%M:\%S` > /tmp/cron.test.log 2>&1. – Gene Aug 27 '15 at 7:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.