0

I have a python script I want to fire off every night at midnight. I'm using cron scheduler right now to do so, however, I can't figure out why it's not working. For now, I've been using close times (within the next minute or so) to test the cronjob, but I will ultimately want it to work for midnight.

Here is what I put in my crontab file (to run for 2:43pm), hosted on my ubuntu machine:

43 14 * * * root /home/grantmcgovern/Developer/Projects/StudyBug/Main.py

I even put the:

#!user/bin/python 

on top of all the .py files.

I also did:

chmod +x "scriptname".py

For each of the .py files and still no luck. Am I missing something blatantly obvious? I should note, this is my first time playing with cron tasks.

carbon_ghost
  • 1,114
  • 5
  • 18
  • 40
  • 2
    The shebang line (the first line in the Python scripts) should look like `#!/usr/bin/python`, not `#!user/bin/python`. – bdesham Apr 18 '14 at 18:51
  • @bdesham Thanks for that catch, still no luck though. – carbon_ghost Apr 18 '14 at 18:54
  • As an alternative to #!/usr/bin/python ... #!/usr/bin/env python – clutton Apr 18 '14 at 18:55
  • @clutton I tried that, unfortunately that did not work either – carbon_ghost Apr 18 '14 at 18:58
  • Try 0 0 * * * root /home/grantmcgovern/Developer/Projects/StudyBug/Main.py if you want to run it at midnight. – Alexander Ejbekov Apr 18 '14 at 18:59
  • Does your syslog show that the cron triggered? – clutton Apr 18 '14 at 19:00
  • 1
    you can log the execution by adding `> /tmp/mycronlog.log` at the end making it `43 14 * * * root /home/grantmcgovern/Developer/Projects/StudyBug/Main.py > /tmp/mycronlog.log`. What does the log say? – evading Apr 18 '14 at 19:02
  • @evading Nothing is in the log – carbon_ghost Apr 18 '14 at 19:06
  • @carbon_ghost - make sure the log is a writeable file in a writeable directory (i.e. you have permissions to run it). Also, in your SSH (if you have access to it), can you run the same command (minus the cron)? – Duniyadnd Apr 18 '14 at 19:07
  • Sorry, you might need to pipe stderr to stdout allso. Try `43 14 * * * root /home/grantmcgovern/Developer/Projects/StudyBug/Main.py > /tmp/mycronlog.log 2>&1` Note the last bit. [edit] also, do what @Duniyadnd says :) – evading Apr 18 '14 at 19:08
  • @evading the thing is though, I wouldn't even need the log file to know if it's running successfully. I'm using selenium, a python web driver library, that physically opens Firefox every time the script is run so I would know if it was running that way. – carbon_ghost Apr 18 '14 at 19:13
  • After the most recent execution, this is the output I got in the log: /bin/sh: 1: root: not found – carbon_ghost Apr 18 '14 at 19:14
  • @carbon_ghost Maybe so, but the log file might tell you where the script crashes. – evading Apr 18 '14 at 19:14
  • 1
    That is telling you that you are trying to run a program named root. – evading Apr 18 '14 at 19:17
  • @carbon_ghost if my answer below helped you, please mark it as accepted :) – marven Apr 19 '14 at 09:32

2 Answers2

2

From your current crontab file, you're basically running root /home/grantmcgovern/Developer/Projects/StudyBug/Main.py every time.

If you want to run it as root, use sudo crontab -e and put 43 14 * * * /usr/bin/python /home/grantmcgovern/Developer/Projects/StudyBug/Main.py instead.

marven
  • 1,836
  • 1
  • 17
  • 14
1

I think it is looking for the command "root" so the syntax is wrong, so it should be this...

43 14 * * * /home/grantmcgovern/Developer/Projects/StudyBug/Main.py

If you need it to run as root then I think you can use su like this:

43 14 * * * su root -c "/home/grantmcgovern/Developer/Projects/StudyBug/Main.py"

If you add it to the system crontab then I think it will anyway.

clutton
  • 620
  • 1
  • 8
  • 18