Python Script Quits Running on Raspberry Pi

Yesterday an expert help me get my script to stop delivering multiple messages. He did a great job! I would have contacted him directly, but this is the only way I know how to.

My script should run 24/7. However, after a couple of hours (an educated guess at best). It quits working. The way I know it quits working is because I don't receive the text messages the script sends when the garage door opens and closes.

Any ideas?

#! /usr/bin/env python
import commands
import smtplib
from email.MIMEText import MIMEText

ser = 'gpio read 1'  #Change /dev/ttyACM0 to your com port

GMAIL_LOGIN = '***********@gmail.com'
GMAIL_PASSWORD = '************'
SEND_TO = '**********@txt.att.net'
 
def send_email(subject, message, from_addr=GMAIL_LOGIN, to_addr=SEND_TO):
    msg = MIMEText(message)
    msg['Subject'] = subject
    msg['From'] = from_addr
    msg['To'] = to_addr
 
    server = smtplib.SMTP('smtp.gmail.com',587) #port 465 or 587
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(GMAIL_LOGIN,GMAIL_PASSWORD)
    server.sendmail(from_addr, to_addr, msg.as_string())
    server.close()

last_result = ""

while 1: #loop forever
   result = commands.getoutput(ser)
   if result.strip() == "1" and result.strip() != last_result:
    status=open('garage.txt', 'w')
    status.write("Garage door is open")
    send_email('OPEN', 'The garage door is open')
    status.close()
    last_result = result.strip();
#    print("open")

   elif result.strip() == "0" and result.strip() != last_result:
    status=open('garage.txt', 'w')
    status.write("Garage door is closed")
    send_email('CLOSED','The garage door is closed')
    status.close()
    last_result = result.strip();
#    print("closed")
dynamicOneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TommySzalapskiCommented:
Does the Raspberry Pi go to sleep or anything? There's nothing about that script that should cause it to stop running unless something crashed or shut down.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dynamicOneAuthor Commented:
No, the Pi stays on 24/7. Could I set it up as a process?
0
dynamicOneAuthor Commented:
Tommy,

This is what the log is saying.

Traceback (most recent call last):
  File "garagestatus2.py", line 31, in <module>
    send_email('OPEN', 'The garage door is open')
  File "garagestatus2.py", line 18, in send_email
    server = smtplib.SMTP('smtp.gmail.com',587) #port 465 or 587
  File "/usr/lib/python2.7/smtplib.py", line 249, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.7/smtplib.py", line 309, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python2.7/smtplib.py", line 284, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "/usr/lib/python2.7/socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known

Is there anything in there that helps?
0
dynamicOneAuthor Commented:
Tommy, after I restarted the Pi the script ran normal again in the terminal. I was able to get crontab to run it as a process now. In the morning I will check if it is still working and let you know.
0
TommySzalapskiCommented:
The log messages make it appear that the device lost internet connectivity in some way. Were there hundreds of those messages in the log or just one?
0
dynamicOneAuthor Commented:
There were many. Yesterday I was having trouble with ssh so I removed and replaced the wifi dongle and couldn't connect at all. Prior to that, it was hit and miss. I inserted an ethernet cable from my laptop to the Pi and tried connecting directly, no. After I removed the ethernet cable it worked. The script is running in crontab and still sends me messages. So far so good!
0
dynamicOneAuthor Commented:
I spoke too soon. Now it won't start in crontab again. I can connect via ssh just fine. When I start the script in terminal it works, but when I exit terminal the script stops working.
0
dynamicOneAuthor Commented:
I had to restart the raspberry pi because I had to move it back to the garage. Do the file permissions change after a restart? This is frustrating because I had a it working and I can't figure out how to get it working again. Here is the crontab entry:

@reboot sudo python /var/www/garagestatus2.py
0
TommySzalapskiCommented:
Do the file permissions change after a restart?
No, but if it prompts for a password for the first sudo command, and then doesn't for successive ones, that could be the issue.
0
dynamicOneAuthor Commented:
how would I check if it is doing that?
0
dynamicOneAuthor Commented:
Here is something I came across in the terminal.

insserv: warning: script 'garagestatus2.py' missing LSB tags and overrides

How can I fix this?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.