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 = "oneemail@email.com"
toEmailAddress = "myemail@gmail.com"
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])[0]

    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:

    # 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
        smtpServer = smtplib.SMTP(smtpHost, smtpPort)
        smtpServer.login(fromEmailAddress, smtpPassword)
        # do nothing

# reboot system

Takephoto.sh is:

    cd /home/pi/camera/photos
gphoto2 --capture-image-and-download --filename "%Y-%m-%dT%H:%M:%SZ.jpg"

and takedebugphotos.sh

    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.

  • Try leaving out the 'env ' in the debug version. So just use LANG=C gphoto2 etcetera – Dirk Dec 11 '18 at 13:59
  • BTW I see a few indentation issues (inside if and while), but they could be copy / paste problems (lines 28 and 36) – Dirk Dec 11 '18 at 14:12
  • Yeah that's just copy paste issues, both of those lines are indented in the code. I tried hashing out the entire takedebugphoto.sh command and it didn't fix it.. so the problem must be something else – agour Dec 11 '18 at 14:21
  • When you have corrected the errors please let us know. We don't want to spend time studying incorrect code. – joan Dec 11 '18 at 14:45
  • Changed the code @joan to make it as-is on the pi. I also tried removing just the 'env' section on takedebugphoto, and it didn't work. – agour Dec 11 '18 at 14:52

Your Answer

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

Browse other questions tagged or ask your own question.