FreeNAS: switch off automatically when not in use

shutdown cronjob
Run the script every 15min to check if there is still a machine running that might need the NAS.

If you use your NAS at home like I do, you don't need it active all the time. Unfortunately, FreeNAS does not have a built-in feature to shut down the NAS when it is not in use - i.e. no computer in the network is online. At FreeNAS Forum and in the PCGH Extreme Forum there are instructions on how to use a cronjob to test regularly (I check every 15 minutes) whether a computer is still responding to a ping.

More problematic is the automatic start. Unfortunately FreeBSD (which is the basis for FreeNAS) version 8.2 does not support Wake-On-LAN with the onboard network card of my HP N36L. This can be solved with a hand patched FreeNAS (which is rather complicated) or with another network card: my choice was the Intel EXPI9301CT PCI Expresswhich, according to various reports, is to be supported and is available in stores for 25,-Euro. The script, which I have adapted a little, is originally from the FreeNAS forum and can be found behind the "more..." link. After that it just needs to be set up as a cronjob.

#!/bin/bash
HOST1=192.168.0.33 #IP of the first PC
HOST2=192.168.0.35 #IP of the second PC
ip_range=192.168.0. #Network address of the own network without the host part
ip=40 #check network from IP address 40

_exit () {
case $1 in
  1) echo "No Shutdown - At least one of the PCs is switched on" ;;
  2) echo "No PC online - Shutdown" ; shutdown -p now ;;
esac
exit $1;
}

# Check if PC's are on
if [ `ping -c 1 -i 1 $HOST1 | grep -wc 100.0%` -eq 0 ] [ `ping -c 1 -i 1 $HOST2 | grep -wc 100.0%` -eq 0 ] ;
then _exit 1; cancel #, a PC is running
# If no PC is on, go to check if PC's in IP range are on
else

# Check if PC's in IP range are on
  while [ $ip -le 45 ] #check network to host unit 45
do
  ping -c 1 -i 1 $ip_range$ip #ping with the composite IP address from the above variables "ip" and "i
  if [ `ping -c 1 -i 1 $ip_range$ip | grep -wc 100.0%` -eq 0 ] #If one of the hosts responds to the ping, do not shut down
then _exit 1 #exit with exit 1 (no shutdown)
fi
 ip=$(( $ip+1 )) 1TP3Height Host portion always by 1
done

_exit 2 # no PC has responded, shutdown
fi

The script has two parts: the first one (up to line 18) checks if one of the fixed computers is on. The part from line 22 on checks a range of IP addresses, in this example from 192.168.0.40 to 192.168.0.45. What you have to keep in mind in general: also devices like iPhone or iPad return a ping and would prevent the NAS from shutting down. Therefore, in most networks, it will be best to check for only a few specific IP addresses and comment out the check for an IP address range.

Important: the call of the - in my case named shutdowncheck.sh - script must be done with absolute path, otherwise the cronjob can't find it. In my case the following must be entered as cronjob:

sh /mnt/Data/shutdowncheck.sh

As soon as the new network card is delivered, I will write an update accordingly, which will be about FreeNAS and Wake-On-Lan (short WOL) - until then, the NAS will be turned on manually when it is needed.

15 comments

  1. Hello,

    great post, just what I was looking for!

    It doesn't work for me, though. I tested it with PuTTY, but this message appears:

    : not found

    /mnt/Data/General/shutdowncheck.sh: 8: Syntax error: expecting "in

    What's wrong with that?

      1. I will post the complete script again tonight - at least for me it works with current FreeNAS. What could be another problem: the rights of the script. It must have 755 to be executable (chmod +755 filename)

        1. Had the same problem when saving the script under Windows, you have to set the editor to save the file with Unix EOL (end of line) otherwise it does not work and prints the error.

  2. Hello

    I have been running the script (similar to this) for about 2 years. Only lately, my nose hangs up during shutdown. If I shut it down by hand and wake it up again with wol, everything works. does anyone have an idea what this could be due to?

    Bs: freenas 8.3.0 release

    1. You mean 9.3.0? Other than that, I currently have the problem that the boot order changes when it shuts down automatically. Shouldn't really happen - maybe it's something similar for you?

      1. No is already 8.3.0 as already written, the script worked fine for a while. But now the NAS hangs up during script shutdown. If I shut down the NAS via WebUi, this will not happen. Do you mean the boot order in the Bios? I can't imagine that this could have a big connection.

        1. hm, but that's strange - I use the script unchanged with 9.3.0 (current stable train) on a HP N36L and it does exactly what it's supposed to.

          The problem with the boot order was that I attached a 5th HDD to the SATA connector for the optical drive. Unfortunately, this then scrambled the boot sequence, for whatever reason.

          1. I'm using that right now:

            #!/bin/bash

            HOST1=192.168.1.3
            HOST2=192.168.1.41
            HOST3=192.168.1.4 HOST4=192.168.1.4
            HOST5=192.168.1.4

            _exit () {
            case $1 in
            1) echo "No Shutdown - At least one of the PCs is switched on" ;;
            2) echo "No PC online - Shutdown" ; shutdown -p now ;;
            esac
            exit $1;
            }

            # Check if PC's are on
            if [ `ping -c 1 -i 1 $HOST1 | grep -wc 100.0%` -eq 0 ] [ `ping -c 1 -i 1 $HOST2 | grep -wc 100.0%` -eq 0 ] [ `ping -c 1 -i 1 $HOST3 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST4 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST5 | grep -wc 100.0%` -eq 0 ] ; then _exit 1;

            # If no PC is on, wait 10 sec if a new one is starting
            else
            echo "No PC online - wait 90 seconds"
            sleep 90

            # check PC's again
            if [ `ping -c 1 -i 1 $HOST1 | grep -wc 100.0%` -eq 0 ] [ `ping -c 1 -i 1 $HOST2 | grep -wc 100.0%` -eq 0 ] [ `ping -c 1 -i 1 $HOST3 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST4 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST5 | grep -wc 100.0%` -eq 0 ] ; then _exit 1;

            # If no PC is on, go to exit case 2 (Shutdown)
            else
            _exit 2
            fi
            fi

  3. Hello,

    i made the script like this on freenas 9.3 and set up the cronjob like this one works so far. But when I start the server the folder and the file with the script is gone. What did I do wrong?

    1. What directory did you put it in? The script must be stored on the data pool, the boot media of FreeNAS is not suitable for this and probably read-only anyway.

      1. I'm using that right now:
        #!/bin/bash

        HOST1=192.168.1.3
        HOST2=192.168.1.41
        HOST3=192.168.1.4 HOST4=192.168.1.4
        HOST5=192.168.1.4

        _exit () {
        case $1 in
        1) echo "No Shutdown - At least one of the PCs is switched on" ;;
        2) echo "No PC online - Shutdown" ; shutdown -p now ;;
        esac
        exit $1;
        }

        # Check if PC's are on
        if [ `ping -c 1 -i 1 $HOST1 | grep -wc 100.0%` -eq 0 ] [ `ping -c 1 -i 1 $HOST2 | grep -wc 100.0%` -eq 0 ] [ `ping -c 1 -i 1 $HOST3 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST4 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST5 | grep -wc 100.0%` -eq 0 ] ; then _exit 1;

        # If no PC is on, wait 10 sec if a new one is starting
        else
        echo "No PC online - wait 90 seconds"
        sleep 90

        # check PC's again
        if [ `ping -c 1 -i 1 $HOST1 | grep -wc 100.0%` -eq 0 ] [ `ping -c 1 -i 1 $HOST2 | grep -wc 100.0%` -eq 0 ] [ `ping -c 1 -i 1 $HOST3 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST4 | grep -wc 100.0%` -eq 0 ] || [ `ping -c 1 -i 1 $HOST5 | grep -wc 100.0%` -eq 0 ] ; then _exit 1;

        # If no PC is on, go to exit case 2 (Shutdown)
        else
        _exit 2
        fi
        fi

        1. Hello,

          I'm trying to run this script (slightly modified by me - the echo was changed) on my Raspberry Pi, but it shows me that at least one PC would still be online, even though all are switched off.

          Do you have any idea what that might be about?
          LG

  4. Hello,
    I am trying to run the script on a XigmaNAS. If I run it as a command everything works correctly, if I start it as a cronjob it will not run. Problem is this line: if [ `ping -c 1 -i 1 $ip_range$ip | grep -wc 100.0%` -eq 0 ]

    Does anyone know the reason?

    Greetings
    Stefan

Leave a Reply

Your email address will not be published. Required fields are marked *