iant1945
Posts: 18
Joined: Fri Oct 05, 2012 9:14 am

crontab sleeping ?

Fri Mar 29, 2013 9:00 am

I'm a RPI newbie (but experienced in PCs). I'm using a clean SD card installation with Wheezy, fully updated, and I haven't changed any settings on the RPI.

My RPI is connected to my burglar alarm via USB. I've written a Python program which interrogates the alarm, downloads a log and emails it to me. It works perfectly when I run it from my PC via Putty.

I then put it into crontab : 30 8 * * * python3 python_files/myapp.py

That worked, as did many variations on the timer, if the forward time was reasonably close. I could shut down Putty and, say, an hour later, the email would arrive. But then I set it up for the following morning (about six hours later) and nothing happened. It's as if crontab had switched off. I then logged back into Putty (without restarting the RPI) - and the program sent me an email both immediately and 45 minutes later with a new crontab time. The problem is the same after several days' attempts.

Any suggestions, please ?

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

Re: crontab sleeping ?

Fri Mar 29, 2013 9:36 am

You should have the full path to python and your program in the crontab.

e.g.

30 8 * * * /usr/bin/python3 /home/pi/python_files/myapp.py

iant1945
Posts: 18
Joined: Fri Oct 05, 2012 9:14 am

Re: crontab sleeping ?

Fri Mar 29, 2013 9:51 am

< You should have the full path .. >

OK, but why does it work without it on short time intervals ?

sdjf
Posts: 1395
Joined: Fri Mar 16, 2012 5:20 am
Location: California
Contact: Website

Re: crontab sleeping ?

Fri Mar 29, 2013 9:58 am

The crontab entry you have posted will only run once per day. If you want to run it hourly, then the entry needs to indicate that. or are you just starting your script with crontab, and expect the script to run continually after that one call?

If you want to call the alarm script once an hour from 8-5, but only on the half hour, the entry would look more like the following:

30 8-5 * * * your-command-here

You have to specify which hours you want it to run, not just the first one.

If the python script is supposed to run continually after that one call at 8:30 a.m, then it is the python script that needs debugging, not your crontab.

There is an alternative way to run hourly jobs using cron, although I have not used it, and that is to put a script into the cron.hourly folder and then it will run every hour without you having to declare it. But it seems that the name of the script cannot have an extension after a period in it (i.e., script.py) or it will not run, so you would have to remove the .py extension from the name, or at least the period if you wanted to use cron.hourly.
FORUM TIP: To view someone's posting history, sign in, click on their user name, then on "Search User's Posts." || Running ArchLinuxArm on Model 2B and 512MB Model B

iant1945
Posts: 18
Joined: Fri Oct 05, 2012 9:14 am

Re: crontab sleeping ?

Fri Mar 29, 2013 10:11 am

< The crontab entry you have posted will only run once per day. >

That's what I'm trying to do - a daily report. But as I said - if I have it run a few hours forward, it works OK, but not when I set it for the following morning.

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

Re: crontab sleeping ?

Fri Mar 29, 2013 10:51 am

iant1945 wrote:I then logged back into Putty (without restarting the RPI) - and the program sent me an email both immediately and 45 minutes later with a new crontab time.
It is more likely to be a delay with the mail, or a problem inside your python script, than with cron. Does the internet connection go down overnight, for instance, or does the alarm refuse to communicate over USB while it is set?

cron should log a line to /var/log/syslog when the job starts. A mail will be sent only if it produces some output. You can check the "Received:" lines in the email headers to see when the job completed and whether the message was delayed en route.

iant1945
Posts: 18
Joined: Fri Oct 05, 2012 9:14 am

Re: crontab sleeping ?

Fri Mar 29, 2013 11:14 am

< cron should log a line to /var/log/syslog when the job starts. >

Thanks, I found that. It says :

Mar 29 06:30:01 raspberrypi /USR/SBIN/CRON[2380]: (pi) CMD (python3 python_files/alarm.py)
Mar 29 06:30:02 raspberrypi /USR/SBIN/CRON[2379]: (CRON) info (No MTA installed, discarding output)

But I don't understand why "No MTA installed", when it's been sending emails without a problem during testing. And there's always a header and body in the email, so it's not that the email is empty.

The router is always on, so shouldn't be an issue over no email server. And during testing, I'm emailing locally to myself.

Does the MTA somehow shut itself down after some time ?

User avatar
rpdom
Posts: 17029
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: crontab sleeping ?

Fri Mar 29, 2013 11:22 am

iant1945 wrote:But I don't understand why "No MTA installed", when it's been sending emails without a problem during testing. And there's always a header and body in the email, so it's not that the email is empty.

And during testing, I'm emailing locally to myself.
I think you just hit the nail on the head with that last part.

By default the MTA (exim4) is set to only deliver local mail. Such as when you use the "mail" command to send to user "pi". It will need a bit more configuration to send to external mail.

You could install sendemail and use that in your script to send the email via an external SMTP server. There is probably a way to do the same thing in python, but I don't know as I don't use python.

iant1945
Posts: 18
Joined: Fri Oct 05, 2012 9:14 am

Re: crontab sleeping ?

Fri Mar 29, 2013 11:38 am

< I think you just hit the nail on the head with that last part. >

Hmm, not certain. I'm testing all the time and all emails are local. The only difference is that I receive the email if the interval between setting up crontab and the target time is relatively short, but not if it's about 5+ hours away.

My program simply does this :

email = MIMEText (report)

email ['Subject'] = email_subject
email ['From'] = email_from
email ['To'] = email_to

server = smtplib.SMTP (email_server)
server.login (email_account, email_password)
server.send_message (email)

Perhaps I have to log-out ? But no doing so hasn't prevented hourly test emails being sent.

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

Re: crontab sleeping ?

Fri Mar 29, 2013 12:50 pm

iant1945 wrote:But I don't understand why "No MTA installed", when it's been sending emails without a problem during testing.
cron cannot send mail without an MTA. You have been sending messages from within your script instead. This is more fragile.

Your code does not appear to have any exception handling or retry logic. If there is even a temporary problem, the script will crash without sending mail. Also, if the script crashes, for any reason, cron will not be able to send you the Python errors.

If cron cannot send mail then you should redirect output instead:

Code: Select all

30 8 * * * python3 python_files/myapp.py >>/tmp/MYAPP 2>&1
Now at least if there are errors they are written to /tmp/MYAPP and not discarded completely.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: crontab sleeping ?

Fri Mar 29, 2013 1:18 pm

A couple of points of clarification:

1) It is not possible that it is a cron and/or crontab problem. And, from the looks of it, the advice about full pathing is, while often correct, off-base in this instance. Please ignore it.

2) It is a problem with your script. And note that there are two kinds of email involved here - the one sent by the cron process upon completion of your job and the one sent internally by your script. It's important to be clear as to which one we're talking about. Question: Are both working? Both failing? Something else?
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

iant1945
Posts: 18
Joined: Fri Oct 05, 2012 9:14 am

Re: crontab sleeping ?

Fri Mar 29, 2013 1:40 pm

< the [email] sent by the cron process upon completion of your job and the one sent internally by your script. >

Sorry, I don't understand. The only email that I'm aware of is the one sent by my program. The crontab command simply runs my program, nothing else.

Anyway, following on from jojopi's suggestion (thank you), I've added a loop to attempt to transmit the email 5 times at one second intervals before failing. I going to test that tonight.

cleverca22
Posts: 581
Joined: Sat Aug 18, 2012 2:33 pm

Re: crontab sleeping ?

Fri Mar 29, 2013 1:46 pm

if your python script gives any output, crontab will try to email that to you, thru seperate channels from those in the script

if that fails, you wont see the output (including any errors python gave)

it helps to end the crontab line with a redirect, to catch all of those
jojopi wrote: If cron cannot send mail then you should redirect output instead:

Code: Select all

30 8 * * * python3 python_files/myapp.py >>/tmp/MYAPP 2>&1
this shows how to do that

Return to “Automation, sensing and robotics”