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.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")
PythonProgramming Languages-OtherProgrammingRaspberry Pi

Avatar of undefined
Last Comment
dynamicOne
ASKER CERTIFIED SOLUTION
Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of dynamicOne
dynamicOne

ASKER

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

ASKER

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?
Avatar of dynamicOne
dynamicOne

ASKER

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.
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?
Avatar of dynamicOne
dynamicOne

ASKER

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!
Avatar of dynamicOne
dynamicOne

ASKER

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.
Avatar of dynamicOne
dynamicOne

ASKER

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.
Avatar of dynamicOne
dynamicOne

ASKER

how would I check if it is doing that?
Avatar of dynamicOne
dynamicOne

ASKER

Here is something I came across in the terminal.

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

How can I fix this?
Programming
Programming

Programming includes both the specifics of the language you’re using, like Visual Basic, .NET, Java and others, but also the best practices in user experience and interfaces and the management of projects, version control and development. Other programming topics are related to web and cloud development and system and hardware programming.

55K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo