This script works when i call it from the command line, but when i have "Motion" call it following the save of a jpg to my PI, it only sends the emails.

I'm mostly get about 10 jpgs, for each event, so wondered if the script was being upset by being called again whilst running. Just a hunch, but how could i test for this, and possibly rectify?

#!/usr/bin/env python

import smtplib
import time
import subprocess

from email.mime.text import MIMEText

MAILTO  = ""

msg = MIMEText('blar blar')
msg['Subject'] = 'from pi script test1'
msg['From'] = USERNAME
msg['To'] = MAILTO

server = smtplib.SMTP('smtp.gmail.com:587')
server.sendmail(USERNAME, MAILTO, msg.as_string())

subprocess.call("cp /mnt/*.jpg  /home/pi/box/pi_pictures", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

2 Answers 2


I found the reason why it wasn't working, I needed to set the permissions on the PI_pictures folder to 0777! for other users!

  • Glad you sorted it out!
    – Chris M
    Mar 6, 2014 at 13:48

Motion runs as the user motion by default, and it sounds as though is running into a permission problem with the file operation.

If this is the case, you could:

  • Set the permissions so that motion can write to your pi_pictures directory (most preferable); or
  • Prefix the cp command with sudo (easiest fix)

Let me know if you want example commands for any of this, or if it doesn't solve your problem.

  • Hi Chris, I've just tried sudo at the front of the command, but still didn't work. I tried running the script from cron, which worked. I wonder if it could be something to do with the fact that i'm copying the files via a script that is being called multiple times. It's trying to copy the same files over themselves and doesn't know what to do? I know the script is being called multiple times, because i get multiple emails, but it hangs when it tries to re copy a file that is in the process of being copied? What do you think?
    – reggie
    Mar 5, 2014 at 7:41
  • You could be right. I would try saving the output of the command, so make a log file e.g. sudo touch ~/cpmotion.log && sudo chown motion:motion ~/cpmotion.log and then edit the cp line to end with > ~/cpmotion.log. Then view the log file after it runs.
    – Chris M
    Mar 5, 2014 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.