chorlton2080
Posts: 124
Joined: Sun Dec 23, 2012 9:44 pm

Cron problems in sending periodic email

Sat Sep 28, 2013 1:59 pm

Hi

I have a Python script which finds the latest jpg in a directory and then emails it out. It is used with a Python motion detect routine, but because of false alarms I don't want the latter script to email every instance of motion it detects: I want a reassuring image every so often.

The emailer script works well when executed from the command line as the default user. I can call the script from any directory using the full absolute description of the location.

I want this script to execute every so often (probably hourly, though I'm testing with a shorter period) automatically, so I assumed cron would be the right tool for this purpose. In this respect I did the following:

Code: Select all

sudo crontab -e
and then added the following (with the same absolute directory reference as above)

Code: Select all

*/5 * * * * sudo python /home/pi/scripts/MotionDetectPython/email-attachment.py
I expect the code to simply execute every 5 mins but it does not. Can anyone identify the problem?

Does cron (when edited as "sudo crontab -e" execute as root and therefore have all permissions?

Thanks in advance

User avatar
joan
Posts: 12738
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Cron problems in sending periodic email

Sat Sep 28, 2013 2:14 pm

cron probably doesn't know where python is located. Put in the full path, probably /usr/bin/python.

chorlton2080
Posts: 124
Joined: Sun Dec 23, 2012 9:44 pm

Re: Cron problems in sending periodic email

Sat Sep 28, 2013 2:52 pm

Well. That worked!

Thank you very much indeed :)

User avatar
jojopi
Posts: 2992
Joined: Tue Oct 11, 2011 8:38 pm

Re: Cron problems in sending periodic email

Sat Sep 28, 2013 3:16 pm

chorlton2080 wrote:Well. That worked!
Both cron and sudo include /usr/bin in their default PATHs. Unless you had explicitly changed these default settings, you are mistaken about what has resolved your issue.

To answer your earlier question, yes "sudo crontab -e" edits root's crontab, so all jobs therein run as root. You do not need the extra sudo within the crontab. It is best to remove it, as well, because in some configurations sudo may require a password, or otherwise refuse to run non-interactively.

chorlton2080
Posts: 124
Joined: Sun Dec 23, 2012 9:44 pm

Re: Cron problems in sending periodic email

Sat Sep 28, 2013 3:25 pm

Thanks folks

I agree that sudo was not required within the crontab, it was put in to eliminate any permissions issues.

I did make a few changes at the same time, so it may be the case that the location of Python may not have needed to be explicity included, but I wasn't sure if PATH was a per-user config: the Pi needs to run without a user logon.

It would be useful if an error file were available for cron (I checked in /var/log; the only thing there is the messages file and no mention of cron errors), but then cron may simply have been content that it had invoked the Python script - the issue may be with Python (can you tell me where the Python log lives?).

Thanks once again!

User avatar
jojopi
Posts: 2992
Joined: Tue Oct 11, 2011 8:38 pm

Re: Cron problems in sending periodic email

Sat Sep 28, 2013 4:05 pm

/var/log/syslog should indicate which cron jobs were attempted. Any output, including errors, from a job is normally sent by email to the local user who owns the crontab. However, that will only work if you install a mail server on the Pi. Often it is more convenient to redirect the output to a file:

Code: Select all

*/5 * * * * python /home/pi/piscript.py >/tmp/piscript.out 2>&1

chorlton2080
Posts: 124
Joined: Sun Dec 23, 2012 9:44 pm

Re: Cron problems in sending periodic email

Sat Sep 28, 2013 4:08 pm

Superb. I've learnt a lot with your help. Thank you

Return to “Raspbian”

Who is online

Users browsing this forum: No registered users and 27 guests