I'm having trouble making a python script run via cronjob.

I can run the python script (it's supposed to connect to a database, pull a few records, save that to a csv, then email it) manually just fine. However, I can't get it to run. It's supposed to run every minute, for testing.

Here's the line in the crontab -e:

* * * * * /usr/bin/python /appl/belo_monthly/belo_monthly.py > /appl/belo_monthly/results.log

I've used chmod -x on the file. I've also tried to write out the print lines of the file to a log. None are working - won't even create .log file.

The python file has #!/usr/bin/python at the very top.

What I tried:

I looked it up here, and found this: Python script not running as cronjob

Using that as a reference:

  1. Yup, cron daemon is running. pidof cron returns a value.
  2. A lot of other services depend on this, so I can't restart it.
  3. sudo tail -f /var/log/syslog gets me the following:

Mar 31 10:21:01 ip-XX-XXX-XXX-XX CRON[30223]: (ubuntu) CMD (/usr/bin/python /appl/belo_monthly/belo_monthly.py > /appl/belo_monthly/results.log)

And it adds another line like that a minute later.

  1. Finally, I tried this: sudo strace -f -p pidof cron. Which returned this:

execve("/bin/sh", ["/bin/sh", "-c", "/usr/bin/python /appl/belo_month"...], [/* 6 vars */]) = 0

And then some garbled mess of text later, this:

open("/appl/belo_monthly/results.log", O_WRONLY|O_CREAT|O_TRUNC, 0666) = -1 EACCES (Permission denied)
write(2, "/bin/sh: 1: ", 12) = 12
write(2, "cannot create /appl/belo_monthly"..., 63) = 63
write(2, "\n", 1)           = 1

So I'm thinking, it can't create a log. But what about the python script? What happened?

  • Try this, stackoverflow.com/questions/8727935/… Hope this could solve your problem. – Vengat Apr 1 '15 at 5:19
  • I'm not sure as to what to try from that question and answer. The cron is working, and I can google how to setup the frequency correctly. For reference, I'm already using #!/usr/bin/python at the top of my python files. – zack_falcon Apr 1 '15 at 7:35

won't even create .log file

Does the user you set the cronjob for have write access to /appl/belo_monthly/results.log? Did you create the cronjob as regular user or as root?

  • I didn't create the cronjob, unfortunately. I don't need to use sudo when editing it, and using root via sudo su and doing crontab -e creates a new, empty cron if it means anything. – zack_falcon Apr 1 '15 at 7:08
  • I added a sudo to the command in the cron job. Finally worked. Though I understand there are / could be some security issues regarding this. – zack_falcon Apr 1 '15 at 7:43

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.