Is it cold outside? Part 3

Last time I’d just finished my weather-checking script, but then I ran into some issues around actually getting it to run! Now we’re going to talk about services!

I first encountered this problem when I started the script the first time on the pi. I connected via SSH, started the script and disconnected, but wondered by I wasn’t getting any notifications. After a bit of research and a note from a friend (thanks Dan!) I found that I needed to create a service. A service is just a scheduled task that runs some action on a Linux machine - in my case, the action was just starting the python script. This had the added benefit of not requiring me to log in and restart the script in the event of an error or a power cycle.

Creating a service is surprisingly easy - just create a file like this: sudo nano /lib/systemd/system/service_name.service and populate it with something like the following:

[Unit]
Description=Service to run temp_check_running script
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python3 /home/pi/weather_check/temp_check_running.py
WorkingDirectory=/home/pi/weather_check
User=pi

Restart=always
RestartSec=60

[Install]
WantedBy=multi-user.target

I’m going to do my best to explain this configuration simply:

[Unit] - The description is pretty simple - it doesn’t show anywhere except this file and it’s meant to give context to anyone who views this file. The After section refers to a point in the boot process where the system is capable of accepting multiple non-graphical sessions and is a pretty common configuration option on systems without a user interface (just like our use case). EDIT: Thanks to a colleague, I now know that network-online.target could be used instead of multi-user.target to ensure that the network interface is online - just flagging that I’ll leave the above as-is, but will be changing my local code.

[Service] - The idle type delays the execution until the system is idle (all other jobs completed so there’s no accidental input/output where it shouldn’t be). ExecStart is just the command to be run with the WorkingDirectory being where the prior line is executed from. User is the user permissions this command will be run with. Restart & RestartSec together result in the service attempting to run every 60seconds upon failure.

[Install] - WantedBy essentially means that this service will be called to start when that service starts.

And there you have it! You can check it’s working by running:

systemctl status service_name.service

And once it’s running, we’ve finished the project! We now have a working script that will continue infinitely and restart in the event it ever stops. That script performs analysis on BoM data and internal sensor data, makes an assessment as to whether it meets the criteria, then notifies via push notification to my phone if necessary. Now I just have to wait for Winter to end before I’ll be able to test if it’s useful!

Thanks for joining me while I worked through this project!

Previous
Previous

What’re you eating? Part 5

Next
Next

Is it cold outside? Part 2