Avatar of dynamicOne
dynamicOne asked on

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.sendmail(from_addr, to_addr, msg.as_string())

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')
    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')
    last_result = result.strip();
#    print("closed")
PythonProgramming Languages-OtherProgrammingRaspberry Pi

Avatar of undefined
Last Comment

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

No, the Pi stays on 24/7. Could I set it up as a process?


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?

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy

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?

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!

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

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

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.

how would I check if it is doing that?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck

Here is something I came across in the terminal.

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

How can I fix this?