I have a script that runs OK in a terminal, but it doesn't run in cron. I've spent the last 5 hours on google and I'm not getting anywhere with it.
The main code is as follows:
import os import smtplib from email.mime.text import MIMEText from datetime import datetime, timedelta import time # actually take the photo os.system("sh /home/pi/camera/takephoto.sh >> /home/pi/camera/logging.txt") # all below is to make sure the above worked pathtophotos = "/home/pi/camera/photos" timeFrame = 2 # in minutes debugCommand = "sh /home/pi/camera/takedebugphoto.sh" # smtp details for error notifications fromEmailAddress = "email@example.com" toEmailAddress = "firstname.lastname@example.org" smtpHost = "smtpout.europe.secureserver.net" smtpPort = 80 smtpPassword = "mypassword" allfilenames = os.listdir(pathtophotos) n = 0 lastPhotoTakenWithinTimeFrame = False while n < len(allfilenames): filename = os.path.splitext(allfilenames[n]) timeTaken = datetime.strptime(filename, "%Y-%m-%dT%H:%M:%SZ") if not (timeTaken < datetime.now() - timedelta(minutes = timeFrame)): lastPhotoTakenWithinTimeFrame = True n += 1 if lastPhotoTakenWithinTimeFrame == False: os.system(debugCommand) # send email when failed # create message message = MIMEText("oh no.\n\nPhoto capture failed at " + datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') + "\n\nRegards,\n\nThe Camera") message["Subject"] = "Photo capture failed" message["From"] = fromEmailAddress message["To"] = toEmailAddress #send message try: smtpServer = smtplib.SMTP(smtpHost, smtpPort) smtpServer.login(fromEmailAddress, smtpPassword) smtpServer.send_message(message) except: pass # do nothing # reboot system time.sleep(5) os.system("reboot")[/code]
cd /home/pi/camera/photos gphoto2 --capture-image-and-download --filename "%Y-%m-%dT%H:%M:%SZ.jpg"
cd /home/pi/camera/photos env LANG=C gphoto2 --debug --debug-logfile=/home/pi/camera/my-logfile.txt --capture-image-and-download --filename="%Y-%m-%dT%H:%M:%SZ.jpg"
The idea is that the script runs and takes a photo. If the photo doesn't work, then it will send me an email and reboot the Pi.
My cron job looked like this:
0,10,20,30 0-24 * * 1-7 /usr/bin/python /home/pi/camera/takephoto.py /home/pi/camera/logging.txt
The script runs on cron, but only the 'take photo' part. The debug section doesn't apparently work in cron.
I've read a load about environmental variables and such, but in all honesty it's all going over my head. I've also run it as a sudo chron job, to no joy.
Could anyone offer some advice?
Edit: When I tell chron to run a log, I get the folllowing text:
Traceback (most recent call last): File "/home/pi/camera/takephoto.py", line 31, in <module> timeTaken = datetime.strptime(filename, "%Y-%m-%dT%H:%M:%SZ") File "/usr/lib/python2.7/_strptime.py", line 332, in _strptime (data_string, format)) ValueError: time data 'takedebugphoto' does not match format '%Y-%m-%dT%H:%M:%SZ'
Strangely, now running the takephoto.py now gives the same error in terminal. I didn't change anything, so not sure why it's broken.
My sudo cronjob now reads as
0,10,20,30 0-24 * * 1-7 /usr/bin/python /home/pi/camera/takephoto.py >>/home/pi/camera/loggingnew.txt 2>&1
But cron doesn't actually output anything to the log file.