/
piwatcher
executable file
·45 lines (34 loc) · 1.48 KB
/
piwatcher
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/bash
# Get your piWatcher hat at https://cat5.tv/piwatcher
# piWatcher works by listening for its program to be called.
# If the hat doesn't receive a heartbeat within 2 minutes,
# power is cut to the Pi.
# This script ensures a heartbeat is sent every 45 seconds, and
# the Pi is powered back on after 15 seconds of power off.
# This way, if the Pi locks up, it will be automatically restarted.
# For those interested, here's how I compile and install piWatcher:
# https://github.com/Cat5TV/nems-admin/blob/master/build/160-rpi-piwatcher
platform=$(/usr/local/bin/nems-info platform)
# If you're not using NEMS Linux but still want to use my script, comment the command above and uncomment this:
#platform=1
if [[ $platform < 10 ]]; then
# Check if a piWatcher is installed
piwatcher=$(/usr/local/bin/piwatcher status 2>&1)
if [[ $piwatcher == *"ERR"* ]]; then
echo 0 > /var/log/nems/piwatcher
echo "No piWatcher board detected. Get yours at https://cat5.tv/piwatcher"
exit
fi
echo 1 > /var/log/nems/piwatcher
# Tell the piWatcher to power off the NEMS Server if it hasn't received a heartbeat for 2 minutes
/usr/local/bin/piwatcher watch 120
# Set piWatcher to re-power the NEMS Server after 15 seconds of power off
/usr/local/bin/piwatcher wake 15
# Prevent piWatcher from shutting down the NEMS server
while true
do
# Send heartbeat to piWatcher every 45 seconds, resetting the 2 minute countdown
/usr/local/bin/piwatcher status
sleep 45
done
fi