I have a script that is meant to work with a UPS Hat for a Raspberry Pi 4b. I am running Ubuntu 21.04 server 64 bit. When I run the script manually from /home/user/ups.sh it works great. However, after following the instructions and creating an rc.local file I cannot get the script to start automatically at boot. I have placed the script into the /user/bin/ directory and done a chmod +x on it.
#!/bin/bash
echo 17 > /sys/class/gpio/export;
echo in > /sys/class/gpio/gpio17/direction;
echo 27 > /sys/class/gpio/export;
echo in > /sys/class/gpio/gpio27/direction;
echo 18 > /sys/class/gpio/export;
echo out > /sys/class/gpio/gpio18/direction;
echo 0 > /sys/class/gpio/gpio18/value;
power_timer=0;
inval_power="0";
ups_online1="0";
ups_online2="0";
ups_online_timer="0";
while true
do
ups_online1=$(cat /sys/class/gpio/gpio27/value);
sleep 0.1;
ups_online2=$(cat /sys/class/gpio/gpio27/value);
ups_online_timer=$((ups_online_timer+1));
if (( "$ups_online1" != "$ups_online2" )); then
ups_online_timer=0;
fi
if (("$ups_online_timer" > 30));
then
echo "$ups_online_timer";
ups_online_timer=30;
power_timer=0;
inval_power=0;
fi
inval_power=$(cat /sys/class/gpio/gpio17/value);
if (( "$inval_power" == 1 )); then
power_timer=$((power_timer+1));
else
power_timer=0;
fi
if (( "$power_timer" == 600 )); then
echo "Powering off..."
sleep 2;
systemctl poweroff;
exit;
fi
done
this is what I have in the rc.local file
#!/bin/sh -e
bash /usr/bin/ups.sh &
exit 0
I have been searching google for days and I have tried cron and systemd files and nothing seems to work. Hopefully there's a kind soul out there that has an answer. Thank you.