Last active
September 4, 2024 10:58
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
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
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.
hehe i had this running when I was living in timor-leste with daily power
outages, worked great. Good luck!
…On Mon, May 30, 2022, 21:02 Bruno Moreira ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
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.
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/5df1af8603d23b46e3cdd7adbd5764c2#gistcomment-4183945>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABAYUCKSKJPNPGBCFO6I2DVMUGERANCNFSM5PSNBHDQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
Thanks for clarification! Will try it as soon as I get my hands on a raspberry pi!
Bruno Moreira
… No dia 07/03/2022, às 23:49, Dimo Fedortchenko ***@***.***> escreveu:
***@***.*** commented on this gist.
Yeah it's a bit unclear, but line 55 is where it's at. ssh powertest . ./powertest.sh so you just create a bash script powertest.sh that echos the phrase on line 22, 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.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.
You are receiving this because you commented.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!