I am using the Cubietruck inside a Ewell case to serve as a portable front-end to listen to music through an attached USB DAC (a Chord Mojo) connected to headphones. I am using a 5300 mAh power from Aliexpress that has the correct 2.5 mm JST connector. I also have a 1TB Samsung SSD inside the case, and am running the latest version of Rune Audio for Cubietruck. Everything is working very well, with excellent, clean sound.
What I am trying to do now is create a script that will shutdown the Cubietruck safely (stopping the MPD service and unmounting the SATA SDD before shutting down) if the battery level reaches a certain level, say 10 percent. (I have already modified the LEDs to indicate when the battery is fully charged or is charging.)
I have cobbled a script from various sources to be located in a file called "/usr/bin/battery_status.sh". The script would be executed as a service "/etc/systemd/system/battery_status.service", containing the following code:
- Code: Select all
[Unit]
Description=battery_status
[Service]
ExecStart=/usr/bin/battery_status.sh
[Install]
WantedBy=multi-user.target
Here is the "battery_status.sh" script:
- Code: Select all
#!/bin/bash
while true
do
battery_level=`cat /sys/class/power_supply/battery/voltage_now`
if [ $battery_level -le 400000 ]; then
echo "Stop MPD and unmount shares..."
mpc stop
systemctl stop nginx
sleep 1
systemctl stop mpd
systemctl stop spopd
sleep 1
umount -aft nfs
umount -aft cifs
sleep 30
systemctl poweroff
fi
sleep 300
done
Essentially, when the battery voltage = 10% of full capacity, the commands from the rune_shutdown.service located in "/var/www/command/rune_shutdown" are executed, and the system then powers off.
My question is, will this script work? Am I missing anything, or doing anything that will screw up the system? Or is there a better way to do this?
Since I am a relative newbie to coding, I don't want to run a script that will end up with some kind of infinite loop or otherwise gum up the works.
Thanks very much,
Rod