Headless and Alone

I’m thrilled with my shiny new Nova R410 and pi 3B setup. My application is a home alarm notification that sends a text message when activated. I’m replacing a 2G device on a PIC microcontroller that ran for about 5 years before T-Mobile pulled the plug on my 2G service. I’m betting that this cloud based solution will last much longer!

Anyhow, this is my first experience with a pi and I run it headless via an Ethernet wire on my Windows laptop. I have been testing the python script and think it is ready to install in my alarm panel. The script is an endless loop and I thought I could run it, unplug the Ethernet wire and say goodby. The alarm panel is in the attic and I don’t go there very often. Sadly, I’ve discovered that the script locks up after a few hours and I think the problem involves the headless configuration. The error message looks like it comes from puTTY. I’d like to keep it headless and use a keyboard interrupt if I ever wanted to stop the script. Any advice appreciated - Larry

PS - as a brand new user I tried my hardest to follow the Hologram python SDK documentation. I deviated from it when I used python3 and hologram.sendSMS instead of cloud.sendSMS. Cloud does not know about csrpsk authentication. It works today and I’m hoping it won’t cause problems in the future.

How are you starting up the script? and how is it locking up? Sorry its just a little unclear as to where or how the script is failing.

Theres an open ticket for us internally to update the documentation as part of a release for the SDK so we are aware that it needs some updating. Python 3 is the correct usage as we converted it over to python 3 almost a year ago

Thank you Dom
As a cautious hobbyist I took small steps. I began with GPIO and a python script that detects change of state. A green LED lights when high and red when low. Keyboard interrupt (^c) breaks out of loop to end script. “python home_alarm.py” starts script and the command prompt disappears. All keyboard input is ignored until I type ^c, which turns off LED and returns command prompt.

My first attempt to make the script run alone was to close the puTTY window, but the LED turned off when connection closed. Then I unplugged the Ethernet wire before closing the puTTY window. This worked fine, but after a few hours I was unable to detect a state change while the green LED was still on. I figured the loop had hung up and the script had not ended. The same hang up occurs if I keep the puTTY connection open with the Ethernet wire intact for several hours. The pi is apparently monitoring the Ethernet connection and is unhappy about inactivity. A windows box pops up that says “puTTY Fatal Error - Network Error: Software Caused Connection Abort”.

Moving along I got WiFi going and hologram.io/python installed. I switched to “sudo python3 home_alarm.py” to start the script after the addition of hologram cloud code. The final script works great, but the pi still has anxiety about being alone. That’s how I got here.

It sounds like you might want to read up on daemonizing processes so they run in the background. The simplest first step here might be the nohup command. This keeps a process running even if the shell that started it goes away. You can then kill your process manually using the kill command.

Like Reuben said, I would make your script run as a service. We do this for some of our network monitors on the chance something goes wrong.

We have a helper function to do just that

import sys
from pathlib import Path

def install_upstart():
    python_path = sys.executable
    module_path = Path(__file__).parent.parent.absolute()
    script = (f"""
[Unit]
Description=<A description>
StartLimitInterval=30
StartLimitBurst=5

[Service]
WorkingDirectory={module_path}
ExecStart={python_path} <your script name>
Restart=on-failure
RestartSec=2
TimeoutStopSec=120

[Install]
WantedBy=multi-user.target
            """)
    with open('/lib/systemd/system/<name your service>.service', 'w') as f:
        f.write(script)
    print('Script installed')

if '--install' in sys.argv[1:]:
    install_upstart()
else:
    # Run your script

you can then install it and enable the service like so:

sudo python3 <script name> --install 
sudo systemctl daemon-reload 
sudo systemctl enable <whatever you called your service>

Shazam shazam, I had some ideas for a solution and am glad I kept them to myself. “nohup sudo python3 home_alarm.py &” from the command prompt in the /my_python directory allows me to run the script, close the puTTY window, disconnect Ethernet and say goodby. The script keeps running and has no hang ups about being alone in the attic. With a beefy battery backup in the alarm panel I’m not too worried about a loss of power reboot and acknowledge the risk. I set up a custom ringtone on my phone when a text is received from the alarm. It is one sound I hope I never hear. Very satisfying project - cheers! Larry

1 Like