User Tools

Site Tools


synologyautoshutdownscript

Synology: Ping dependent DSM auto shutdown via script with Tux

Hey, my name is "Tux"! Did you ever wonder how you can shutdown your Synology NAS automatically when its clients don't need it anymore?

Hey, my name is "Tux" and this tutorial will show you how you can set up a pinging script on your Synology NAS which shuts down your Synology NAS itself whenever specific NAS clients are not reachable anymore over the network within a specific amount of time.



First thoughts

Note

This tutorial is not for everyone! It is basically for people who want their NAS being turned off to save power when it is not in use by its client(s). Mostly this takes place when the NAS is configured as a media server only.


Start of tutorial

Create the following auto-shutdown script:

$ vi /root/pingCheckAutoShutdown.sh

Add the following content (Important: If you copy-paste the following content, make sure you replace the according whitespaces with tabulators. Alternatively type in the script code manually to avoid formatting issues as Linux command line is very(!) sensitive!).
The following example is pinging three (3) NAS clients. Only if all of the three clients are not reachable anymore via ping over network for example an mount of 900 seconds the NAS will shutdown itself. The delaytime variable value decides the interval of the script being repeated after it has reached its end. Feel free to adjust the script according to your requirements!

#!/bin/sh
#
host01="<myClient01IPAddress>"
host02="<myClient02IPAddress>"
host03="<myClient03IPAddress>"
sleeptime=<myWaitTimeInSeconds> #for example "900"
delaytime=<myDelayTimeInSeconds> #for example "60"
#
while true; do
    ping -c 1 "$host01" > /dev/null
    host01Result="$?"
    #echo "Result01: $host01Result"
    ping -c 1 "$host02" > /dev/null
    host02Result="$?"
    #echo "Result02: $host02Result"
    ping -c 1 "$host03" > /dev/null
    host03Result="$?"
    #echo "Result03: $host03Result"
    if [ "$host01Result" == 1 ] && [ "$host02Result" == 1 ] && [ "$host03Result" == 1 ]; then
        sleep "$sleeptime"
        ping -c 1 "$host01" > /dev/null
        host01Result="$?"
        #echo "Result001: $host01Result"
        ping -c 1 "$host02" > /dev/null
        host02Result="$?"
        #echo "Result002: $host02Result"
        ping -c 1 "$host03" > /dev/null
        host03Result="$?"
        #echo "Result003: $host03Result"
        if [ "$host01Result" == 1 ] && [ "$host02Result" == 1 ] && [ "$host03Result" == 1 ]; then
            poweroff
        fi
    fi
    sleep "$delaytime"
done;

Save the file and quit the editor.

Now make the script executable:

$ chmod +x /root/pingCheckAutoShutdown.sh

Now quit the SSH session.

Now configure the script to run on every NAS system start via Synology's Task Scheduler (Triggered Task).


End of tutorial

synologyautoshutdownscript.txt · Last modified: 2020/11/03