Melissa
Posts: 10
Joined: Thu Apr 02, 2015 12:43 pm

Problem with Crontab

Thu Apr 02, 2015 1:22 pm

Hello

I have a Problem with the crontab function.
I wrote the following python Script that only sends an email, and it worked when i called it from the terminal with python mail_script.py

Code: Select all

import smtplib

mailServer = 'pop.gmail.com'
mailPort = 587
mailLogin = 'example@example.com'
mailPass = 'password'
mailSendFrom = mailLogin
mailSendTo = [u]'receiver@receiver.com'[/u]
mailTLS = True
mailDebug = False

def sendemail(from_addr, to_addr, subject, message):
    try:
        header = 'From: %s\n' % from_addr
        header+= 'To: %s\n' % to_addr
        header+= 'Subject: %s\n\n' % subject
        message = header + message
        conn = smtplib.SMTP(mailServer, mailPort)
        if mailDebug:
            conn.set_debuglevel(True) #show communication with the server
        if mailTLS:
            conn.starttls()
        conn.login(mailLogin, mailPass)
        error = conn.sendmail(from_addr, to_addr, message)
        if not error:
            print "Successfully sent email"
    except Exception, e:
        print "\nSMTP Error: " + str(e)
    finally:
        if conn:
            conn.quit()

if __name__ == '__main__':
          sendemail(mailSendFrom, mailSendTo, 'hi', 'How are you?') 
But when i tried to run it with crontab every minute, i received a mail from the mailer-daemon@googlemail.com.

In that mail was written:

Delivery to the following recipient failed permanently:

pi@raspberrypi

Technical details of permanent failure:
DNS Error: Address resolution of raspberrypi. failed: Domain name not found



Know I am very confused.. the recepient of my python script is receiver@receiver.com, and not pi@raspberrypi.
So I don't know why the mail was tried to send to pi@raspberrypi when called the script with crontab?

The same happens, when I try to call another script with crontab, for example "helloworld.py". I get an email with the same error..

When I call the script in the terminal with python mail_script.py, everything is ok and the mail gets sended to receiver@receiver.com..

User avatar
bobstro
Posts: 193
Joined: Wed Feb 05, 2014 6:48 am
Location: Central Massachusetts, US
Contact: Website

Re: Problem with Crontab

Thu Apr 02, 2015 1:59 pm

Keep in mind that programs fired off from crontab do not inherit the same environment as a login shell. If a script works at the shell but not from crontab, you are likely missing PATH entries and other environment variables. You might also have to specify the full path to your program in your crontab entry.

When a cron job fires off, any output is sent to the owner of the job. In your case, I suspect it's firing off the job and attempting to send an email to your user (pi@raspberrypi) and failing because you don't have email fully set up.

Melissa
Posts: 10
Joined: Thu Apr 02, 2015 12:43 pm

Re: Problem with Crontab

Thu Apr 02, 2015 3:40 pm

Ok thanks for your advice!
I got another error: The crontab should call a python script. And that script should create and edit a pickle file. Then I received the error, that the permission to a pickle file was denied for the crontab.

Code: Select all

Traceback (most recent call last):
  File "/home/pi/test.py", line 16, in <module>
    write()
  File "/home/pi/test.py", line 12, in write
    file = open("/home/pi/test_datei.pkl","wb")
IOError: [Errno 13] Permission denied: '/home/pi/test.pkl'
I thought about to give all rights to the test.pkl file, with chmod 777, but it doesn't exist before i called the script with cron..
How can i fix this error?
Or do i have to edit the sudoers file?

Thank for your help

User avatar
bobstro
Posts: 193
Joined: Wed Feb 05, 2014 6:48 am
Location: Central Massachusetts, US
Contact: Website

Re: Problem with Crontab

Thu Apr 02, 2015 5:19 pm

Are you using a system-wide crontab, or a user-specific one? It may be that the cron jobs are running as a different user (root most likely) than when you run them at the terminal. Be sure the job is running as the owner of /home/pi.

Melissa
Posts: 10
Joined: Thu Apr 02, 2015 12:43 pm

Re: Problem with Crontab

Thu Apr 02, 2015 6:17 pm

I'm not sure..
I just wrote in the terminal crontab -e and wrote the needed line in the crontab file.
So I think it's a system wide crontab.

How can I make sure that the job is running as the owner of /home/pi ?

Thanks again for your help!

User avatar
mad-hatter
Posts: 419
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside

Re: Problem with Crontab

Thu Apr 02, 2015 6:53 pm

Hello,

This explains:-
http://www.raspberrypi.org/forums/viewt ... 9&p=348997
A post by DirkS
...by using either 'crontab -e' (for current user) or 'sudo crontab -e' (for root)

Regards

Melissa
Posts: 10
Joined: Thu Apr 02, 2015 12:43 pm

Re: Problem with Crontab

Thu Apr 02, 2015 7:22 pm

Ooohhh great, it works now!! :D :D
I had to edit it with sudo crontab -e

Thanks to both of you!!
Thank you, Thank you! :)

Return to “Troubleshooting”