2019-06-04 10:09:40 +01:00
|
|
|
#!/bin/bash
|
2021-08-04 00:18:30 +02:00
|
|
|
# for reboot call: sudo /home/admin/config.scripts/blitz.shutdown.sh reboot
|
2019-12-11 14:10:34 +01:00
|
|
|
|
2021-08-27 03:59:21 -04:00
|
|
|
# use this script instead of direct shutdown command to:
|
2019-12-11 14:10:34 +01:00
|
|
|
# 1) give UI the info that a reboot/shutdown is now happening
|
|
|
|
# 2) shutdown/reboot in a safe way to prevent data corruption
|
2019-06-04 10:09:40 +01:00
|
|
|
|
2023-12-13 12:11:13 +01:00
|
|
|
# check if sudo
|
|
|
|
if [ "$EUID" -ne 0 ]; then
|
|
|
|
echo "Please run as root (with sudo)"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-12-14 23:34:35 +01:00
|
|
|
source <(/home/admin/_cache.sh get network)
|
2019-06-04 10:09:40 +01:00
|
|
|
|
2019-12-11 13:48:29 +01:00
|
|
|
# display info
|
2023-12-13 12:11:13 +01:00
|
|
|
echo
|
|
|
|
rpiModel=$(cat /proc/device-tree/model 2>/dev/null | tr -d '\0')
|
|
|
|
if [ -n "${rpiModel}" ]; then
|
|
|
|
echo "${rpiModel}"
|
|
|
|
if echo ${rpiModel} | grep -Eq 'Raspberry Pi 4'; then
|
|
|
|
echo "When shutdown is complete the green activity light stays dark and the LCD turns white on the ${rpiModel}."
|
|
|
|
elif echo ${rpiModel} | grep -Eq 'Raspberry Pi 5'; then
|
|
|
|
echo "When shutdown is complete the activity light turns red and the LCD turns white on the ${rpiModel}."
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2019-12-11 13:48:29 +01:00
|
|
|
if [ "$1" = "reboot" ]; then
|
|
|
|
shutdownParams="-h -r now"
|
|
|
|
echo "It will then reboot again automatically."
|
2021-12-14 23:34:35 +01:00
|
|
|
/home/admin/_cache.sh set state "reboot"
|
|
|
|
/home/admin/_cache.sh set message "$2"
|
2019-12-11 13:48:29 +01:00
|
|
|
else
|
2023-12-04 19:07:25 +01:00
|
|
|
shutdownParams="-P -h now"
|
2019-12-11 13:48:29 +01:00
|
|
|
echo "Then wait 5 seconds and disconnect power."
|
2021-12-14 23:34:35 +01:00
|
|
|
/home/admin/_cache.sh set state "shutdown"
|
|
|
|
/home/admin/_cache.sh set message ""
|
2019-12-11 13:48:29 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# do shutdown/reboot
|
2019-06-04 10:09:40 +01:00
|
|
|
echo "-----------------------------------------------"
|
2021-09-30 11:17:58 +02:00
|
|
|
sleep 3
|
2021-01-12 19:15:31 +01:00
|
|
|
|
2023-12-13 12:11:13 +01:00
|
|
|
# general services to stop
|
|
|
|
servicesToStop="electrs fulcrum"
|
|
|
|
for service in ${servicesToStop}; do
|
|
|
|
if systemctl is-active --quiet ${service}; then
|
|
|
|
echo "stopping ${service} - please wait .."
|
|
|
|
timeout 120 systemctl stop ${service}
|
|
|
|
fi
|
|
|
|
done
|
2021-01-12 19:15:31 +01:00
|
|
|
|
2023-12-13 12:11:13 +01:00
|
|
|
# lndg
|
2022-12-09 21:49:44 +01:00
|
|
|
# stopping LNDg (if installed)
|
|
|
|
isInstalled=$(sudo ls /etc/systemd/system/jobs-lndg.service 2>/dev/null | grep -c 'jobs-lndg.service')
|
|
|
|
if ! [ ${isInstalled} -eq 0 ]; then
|
|
|
|
echo "stop LNDg - please wait .."
|
2023-12-13 12:11:13 +01:00
|
|
|
timeout 120 systemctl stop gunicorn.service 2>/dev/null
|
|
|
|
timeout 120 systemctl stop jobs-lndg.timer 2>/dev/null
|
|
|
|
timeout 120 systemctl stop jobs-lndg.service 2>/dev/null
|
|
|
|
timeout 120 systemctl stop rebalancer-lndg.timer 2>/dev/null
|
|
|
|
timeout 120 systemctl stop rebalancer-lndg.service 2>/dev/null
|
|
|
|
timeout 120 systemctl stop htlc-stream-lndg.service 2>/dev/null
|
2022-12-09 21:49:44 +01:00
|
|
|
fi
|
|
|
|
|
2023-12-13 12:11:13 +01:00
|
|
|
# lightning
|
|
|
|
lightningServicesToStop="lnd tlnd slnd lightningd tlightningd slightningd"
|
|
|
|
for service in ${lightningServicesToStop}; do
|
|
|
|
if systemctl is-active --quiet ${service}; then
|
|
|
|
echo "stopping ${service} - please wait .."
|
|
|
|
timeout 120 systemctl stop ${service}
|
|
|
|
fi
|
|
|
|
done
|
2021-01-12 19:15:31 +01:00
|
|
|
|
2023-12-13 12:11:13 +01:00
|
|
|
# bitcoind
|
2022-01-12 14:03:18 +01:00
|
|
|
if [ "${network}" != "" ]; then
|
|
|
|
# stopping bitcoin (thru cli)
|
|
|
|
echo "stop ${network}d (1) - please wait .."
|
|
|
|
timeout 10 sudo -u bitcoin ${network}-cli stop 2>/dev/null
|
|
|
|
|
|
|
|
# stopping bitcoind (thru systemd)
|
|
|
|
echo "stop ${network}d (2) - please wait .."
|
2023-12-13 12:11:13 +01:00
|
|
|
timeout 120 systemctl stop ${network}d 2>/dev/null
|
|
|
|
timeout 120 systemctl stop t${network}d 2>/dev/null
|
|
|
|
timeout 120 systemctl stop s${network}d 2>/dev/null
|
2022-01-12 14:03:18 +01:00
|
|
|
sleep 3
|
|
|
|
else
|
|
|
|
echo "skipping stopping layer1 (network=='' in cache)"
|
|
|
|
fi
|
2020-01-15 12:05:26 +01:00
|
|
|
|
|
|
|
# make sure drives are synced before shutdown
|
2023-12-13 12:11:13 +01:00
|
|
|
source <(/home/admin/config.scripts/blitz.datadrive.sh status)
|
2021-09-12 17:45:57 +02:00
|
|
|
if [ "${isBTRFS}" == "1" ] && [ "${isMounted}" == "1" ]; then
|
2020-01-15 12:05:26 +01:00
|
|
|
echo "STARTING BTRFS RAID DATA CHECK ..."
|
2023-12-13 12:11:13 +01:00
|
|
|
btrfs scrub start /mnt/hdd/
|
2020-01-15 12:05:26 +01:00
|
|
|
fi
|
2019-06-04 10:09:40 +01:00
|
|
|
sync
|
2020-01-15 12:05:26 +01:00
|
|
|
|
2023-12-04 19:07:25 +01:00
|
|
|
# unmount HDD - try to kill all processes first #3114
|
|
|
|
echo "# Killing the processes using /mnt/hdd"
|
2023-12-13 12:11:13 +01:00
|
|
|
processesUsingDisk=$(lsof -t "/mnt/hdd")
|
2023-12-08 23:16:17 +01:00
|
|
|
if [ -n "$processesUsingDisk" ]; then
|
|
|
|
while read -r pid; do
|
|
|
|
processName=$(ps -p $pid -o comm=)
|
|
|
|
echo "# Stop $processName with: 'kill -SIGTERM $pid'"
|
2023-12-13 12:11:13 +01:00
|
|
|
kill -SIGTERM $pid # Send SIGTERM signal
|
|
|
|
sleep 5 # Wait for the process to terminate
|
|
|
|
done <<<"$processesUsingDisk"
|
2023-12-08 23:16:17 +01:00
|
|
|
fi
|
|
|
|
|
2023-12-04 19:07:25 +01:00
|
|
|
echo "# Attempt to unmount /mnt/hdd"
|
2023-12-13 12:11:13 +01:00
|
|
|
umount "/mnt/hdd"
|
2023-12-04 19:07:25 +01:00
|
|
|
|
2019-06-04 10:09:40 +01:00
|
|
|
echo "starting shutdown ..."
|
2023-12-13 12:11:13 +01:00
|
|
|
shutdown ${shutdownParams}
|
2022-01-08 20:55:24 +01:00
|
|
|
|
2023-12-04 19:07:25 +01:00
|
|
|
# detect missing DBUS
|
2022-01-17 00:29:20 +01:00
|
|
|
if [ "${DBUS_SESSION_BUS_ADDRESS}" == "" ]; then
|
2022-01-08 20:55:24 +01:00
|
|
|
echo "WARN: Missing \$DBUS_SESSION_BUS_ADDRESS .. "
|
|
|
|
if [ "$1" = "reboot" ]; then
|
|
|
|
echo "RUNNING FALLBACK REBOOT .. "
|
2023-12-13 12:11:13 +01:00
|
|
|
systemctl --force --force reboot
|
2022-01-08 20:55:24 +01:00
|
|
|
else
|
|
|
|
echo "RUNNING FALLBACK SHUTDOWN .. "
|
2023-12-13 12:11:13 +01:00
|
|
|
systemctl --force --force poweroff
|
2022-01-08 20:55:24 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-12-09 21:49:44 +01:00
|
|
|
exit 0
|