Raspian/Debian Thermometer Update – SystemD Timer

Hi this an update to a post from a couple of years ago:

https://blog.tomsdomain.co.uk/2018/11/25/raspbian-debian-thermometer/

Specifically the scheduling of a job to run the temperature check & send email email alert if required, with a SystemD timer rather than a Cron job. Also removing the requirement for a separate python script. The temperature check and alert is now all done with one bash script.  The script does now require installing msmtp, this does require some configuration to work with an external mail server (as did the python script in the original post); a quick google search should give you all you need; if not please comment and I’ll assist further.

Updated bash script (/home/pi/temp-check.sh):

#!/bin/bash
TEMP=$(/usr/local/bin/tempered | grep -oP '(?<=/dev/hidraw1 0: temperature) [\d.]+')
MAXTEMP=40
if [ $TEMP \> $MAXTEMP ];
then
echo -e "Subject: $TEMP°c Temperature is to high! \r\n\r\n$TEMP°c Temperature is to high!" |msmtp --from=from@example.com -t to@example.com
else
echo "Temperature" $TEMP"°c, normal";
fi;
unset TEMP MAXTEMP

Just set the MAXTEMP variable, to the maximum desired temperature.

Now the SystemD timer, first thing is to create the service unit (/lib/systemd/system/check-temp.service):

[Unit]
Description=Temperature Check

[Service]
Type=simple
ExecStart=/home/pi/temp-check.sh

[Install]
WantedBy=multi-user.target

Next the timer unit is created (/lib/systemd/system/check-temp.timer):

[Unit]
Description=Check Temperature Timer

[Timer]
OnCalendar=*:0/15
Unit=check-temp.service

[Install]
WantedBy=timers.target

Here the timer is scheduled to run every 15 minutes, this can be increased and decreased, more details can be found here.

To schedule the job, please run:
$ sudo systemctl start check-temp.timer

So it persists after a reboot please issue:
$ sudo systemctl enable check-temp.timer

And that’s it, temparature check and aleart should now be scheduled via a SystemD timer.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.