Skip to content

Instantly share code, notes, and snippets.

@zeraien
Last active September 4, 2024 10:58
Show Gist options
  • Save zeraien/5df1af8603d23b46e3cdd7adbd5764c2 to your computer and use it in GitHub Desktop.
Save zeraien/5df1af8603d23b46e3cdd7adbd5764c2 to your computer and use it in GitHub Desktop.
Shut down your "dumb" UPS-powered Synology when there is a power outage
#!/bin/bash
# If you are too cheap (like me) to buy an expensive "smart" UPS,
# this script will allow you to use a Raspberry Pi or any SSH-enabled device
# to automatically shut down your DiskStation when the Pi loses power.
# Meaning you simply plug the "heartbeat device" into regular power,
# and your DiskStation into a UPS, and when the regular power is dead,
# the heartbeat device will die thus your synology will be unable to reach it
# and shut down in due time (10min default).
# This script will check if a given ssh command responds
# with the correct phrase.
# If correct phrase is given, a timestamp is put into a text file.
# The timestamp is the time at which the machine should shutdown.
# A simple shell script called "powertest.sh" is placed on an
# SSH server, in this case a raspberry pi that is plugged into the
# regular grid power. If grid power goes down, the raspberry pi
# will die, so this script will not receive a reply, in which case
# the shutdown will occur 10 minutes since the last successful
# connection.
# HOSTNAME or IP of your heartbeat machine
HEARTBEAT_IP="powertest"
# the phrase that the heartbeat machine should respond with
TEST_PHRASE="power is on"
# the shutdown command
SHUTDOWN_CMD="synoshutdown --shutdown"
# the file where the scheduled shutdown time is stored
# this file just stores epoch seconds, when time is reached
# the shutdown will commence.
TIME_FILE=~/power_down_at
# how long can heartbeat not respond?
GRACE_SECONDS=600
# Synology DiskStation NAS LED and Beeps
# You can make your diskstation beep or change the LED state from the command line.
# Must be run as root.
# Example to make a long beep: `# echo 3 > /dev/ttyS1`
#
# Echo one of these for the desired effect:
# 2 - short beep
# 3 - longer beep
# 4 - blue power LED on
# 5 - blue power LED blink
# 6 - blue power LED off
# 7 - status LED off
# 8 - status LED on
# 9 - status LED blink
function signal() {
echo $1 > /dev/ttyS1
}
NEXT_AT=$((`date +'%s'`+$GRACE_SECONDS))
if [[ -e $TIME_FILE ]]; then
RESULT="`ssh $HEARTBEAT_IP . ./powertest.sh`"
NOW=(`date +'%s'`)
POWER_DOWN_AT=(`cat $TIME_FILE`)
REMAINING=$(($POWER_DOWN_AT - $NOW))
echo "Seconds until shutdown: $REMAINING"
if [[ $REMAINING -lt 30 ]]; then
# extra final heartbeat warning
signal 3
sleep 1
fi
if [[ $REMAINING -lt -300 ]] || [[ $RESULT == $TEST_PHRASE ]]; then
# normal state - set shutdown time to 10 minutes from now
signal 4
signal 8
echo $NEXT_AT > $TIME_FILE
elif [[ $REMAINING -lt 0 ]]; then
# the end, shutdown!
echo "Will power down now, running $SHUTDOWN_CMD."
echo `$SHUTDOWN_CMD`
else
# warn user that we do not detect a heartbeat
signal 2
signal 2
signal 9
echo "No connection to dead man's switch - $REMAINING seconds until shutdown."
fi
else
echo $NEXT_AT > $TIME_FILE
fi
@bmscmoreira
Copy link

Can you be more specific on how to operationalize this? My guess is this should be runing as a CRON in Synology, but how does this interact with Rpi? (I see no IP). Thanks!

@zeraien
Copy link
Author

zeraien commented Mar 7, 2022

Yeah it's a bit unclear, but line 58 is where it's at. ssh powertest . ./powertest.sh so you just create a bash script powertest.sh that echos the phrase on line 25, copy it to the hearbeat machine and then replace powertest with the IP or hostname of the machine, this can be a pi or pretty much any machine that responds to SSH that powers down in case of a power outage.

PS: I updated the GIST to make it a bit more clear

@bmscmoreira
Copy link

Finally got my hands on a Pi and will try this! Pressure is on: got a power outage a few days ago and had to manually shutdown my Synology.

@zeraien
Copy link
Author

zeraien commented May 31, 2022 via email

@bmscmoreira
Copy link

bmscmoreira commented Oct 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment