I have written a python program that continuously parses my current IP and emails it to me if it changes that I want to begin running as soon as the computer boots up: /home/pi/sendIP.py
#note that unnecessary comments and sensitive information have been stripped
#assume this code works as intended
import smtplib
import os
import re
import time
def mail(message):
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login('from_email', 'password')
server.sendmail('from_email', 'to_email', '\nIP: ' + str(message))
server.close()
return True
except:
return False
def getStringIP():
try:
IP = re.search(r'((?:\d{1,3}.){3}\d{1,3})', os.popen('hostname -I').read()).group(1)
return IP
except:
return "null"
ip = ""
while(True):
try:
newIP = getStringIP()
while(newIP == "null"):
time.sleep(1)
newIP = getStringIP()
if(ip == newIP):
time.sleep(1)
else:
ip = newIP
while(not mail(ip)):
time.sleep(0.5)
time.sleep(1)
except:
time.sleep(2)
This file is run by /home/pi/sendIP.sh
#!/bin/bash
echo "calling sendIP.py"
/usr/bin/python3 /home/pi/sendIP.py&
echo "called in separate process"
sendIP.sh
is in turn called by sendIP.service
when the service is started manually or on bootup.
If I call sendIP.sh
directly by doing sudo bash sendIP.sh
in a terminal window, it works perfectly and I receive an email with my current local IP.
If, however, I do sudo systemctl start sendIP.service
, it does not run the python file.
Here is the .service file: /etc/systemd/system/sendIP.service
[Unit]
Description=Sends IP to my cellphone
After=all
[Service]
ExecStart=/bin/bash /home/pi/sendIP.sh
StandardOutput=inherit
StandardInput=inherit
User=pi
[Install]
WantedBy=multi-user.target
And here is the result of sudo systemctl status sendIP.service
after attempting to start the service:
● sendIP.service - Sends IP to my cellphone Loaded: loaded (/etc/systemd/system/sendIP.service; enabled) Active: inactive (dead) since Sun 2018-05-06 20:10:45 UTC; 1min 40s ago Process: 9830 ExecStart=/bin/bash /home/pi/sendIP.sh (code=exited, status=0/SUCCESS) Main PID: 9830 (code=exited, status=0/SUCCESS)
May 06 20:10:44 raspberrypi systemd[1]: Started Sends IP to my cellphone. May 06 20:10:45 raspberrypi bash[9830]: calling sendIP.py May 06 20:10:45 raspberrypi bash[9830]: called in separate process
It looks like it runs, as it echoes the text from the .sh file, but I do not receive any email as I should... I tried using print()
statements in my python file to see if it even begins execution, but it does not print anything to my terminal window—including the echo
statements, which I can only see when I check the status of the service (I am using ssh to access it, I don't know if it opens another window and prints there but it shouldn't). Running sendIP.sh
directly does print both echo
statements in the .sh file and and any print
statements in the python program to the console, even with the &
character causing sendIP.py
to execute in another process.
I should admit that I do not really know what I am doing with the .system file configuration, that part is mostly just what I have pieced together from similar questions and responses. If anyone could help me out that would be great! Thank y'all so much!