Hobbit36
Hobbit36

Reputation: 275

Crontab Python Script not running

I know this question has been asked before but I still haven't been able to get it to work. My crontab file just has this:

0 5 * * * /home/harry/my_env/bin/python /home/harry/compile_stats/process_tonight.py 

Here's what my process_tonight.py looks like:

import datetime
import sys
sys.path.append('/home/harry/compile_stats/')
import compile   # Module in above path

print("Processing last night\n")

date = str(datetime.datetime.today().year) + "-" + str(datetime.datetime.today().month) + "-" + str(datetime.datetime.today().day-1)

compile.process(date, date)

This file works perfectly fine when I just run it regularly from the command line but doesn't work when I schedule it.

I also looked at my /var/log/syslog file and the task I'm looking to run isn't showing up there.

Any ideas?

EDIT: The time it's set to run in my example (5 A.M) is just a random time to put in. It's not running for any time I put in there.

EDIT 2#:

As per user speedyturkey I simplified my python script to better diagnose the problem:

import datetime
#import sys
#sys.path.append('/home/harry/compile_stats/')
#import compile   # Module in above path

print("Processing last night\n")

date = str(datetime.datetime.today().year) + "-" + str(datetime.datetime.today().month) + "-" + str(datetime.datetime.today().day-1)

#compile.process(date, date)

Nothing is happening so I guess the problem isn't with the import.

Upvotes: 0

Views: 2883

Answers (5)

Tyler Tresslar
Tyler Tresslar

Reputation: 393

Have you tried running it from a shell script? I was having the same issue with my python script. I ended up putting the command in a shell script and running that. It threw an error that the library wasn't imported, so I installed it with pip and the --user flag. Now cron runs the shell script no issues.

Upvotes: 0

Federricco
Federricco

Reputation: 129

I know it's a bit silly, but checking your system time/timezone might be helpfull ))) I set my job to run at 5AM and when I logged in at 8AM there was no result of my script. So I spent over 1 hour trying to figure out what's the problem before I noticed that system time is incorrect and 5AM haven't come yet.

Upvotes: 0

speedyturkey
speedyturkey

Reputation: 136

Instead of trying to modify the path from within your python script, you could do something like:

cd /home/harry/compile_stats/ && ./process_tonight.py

which would make it easier to import compile correctly. Note that this would also require making process_tonight.py executable (chmod +x process_tonight.py) and adding a shebang pointing to your Python interpreter (I guess... #!/home/harry/my_env/bin/python).

EDIT in response to Edit #2 above: It is actually not possible to tell if it is running from the code you have written - the print statements are not being redirected. I suggest changing the code to perform some kind of side effect that you can check. For example, import subprocess and then do (example):

subprocess.call("date > /home/harry/compile_stats/date.txt")

If the script is executed properly, it will redirect the output of date to the file specified.

Upvotes: 0

Hobbit36
Hobbit36

Reputation: 275

Ok, I was able to get it to work by creating a specific cron file, putting the info and there and loading it in.

So process_tonight.cron contains this:

0 5 * * * /home/harry/my_env/bin/python /home/harry/compile_stats/process_tonight.py

And I just loaded it into crontab:

crontab process_tonight.cron

Not really sure why this works and the other way doesn't (maybe someone else has an idea).

Upvotes: 1

Xorgon
Xorgon

Reputation: 520

As per the comments, I believe the problem is in how you are calling the python script in the crontab. Run the exact command you've given crontab and fix any problems it returns.

Upvotes: 1

Related Questions