Automatic shutdown after network inactivity

For my NAS, I tried a few options to go more green and save some energy. Besides the WOL which works well, I set up the NAS to shut down after some time of network inactivity (or at least few activity). To achieve that, I digged through the net and found a solution somewhere (probably here) which I adapted to my needs.

I named the script shutdown_if_inactive and put it to /usr/local/sbin/. The script is as follows:


#!/bin/bash
#
# This is scheduled in CRON. It will run every 20 minutes
# and check for inactivity. It compares the RX and TX packets
# from 20 minutes ago to detect if they significantly increased.
# If they haven't, it will force the system to sleep.
#

log=~/scripts/idle/log

# Extract the RX/TX
rx=`/sbin/ifconfig eth0 | grep -m 1 RX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`
tx=`/sbin/ifconfig eth0 | grep -m 1 TX | cut -d: -f2 | sed 's/ //g' | sed 's/errors//g'`

#Write Date to log
date >> $log
echo "Current Values" >> $log
echo "rx: "$rx >> $log
echo "tx: "$tx >> $log

# Check if RX/TX Files Exist
if [ -f ~/scripts/idle/rx ] || [ -f ~scripts/idle/tx ]; then
p_rx=`cat ~/scripts/idle/rx` ## store previous rx value in p_rx
p_tx=`cat ~/scripts/idle/tx` ## store previous tx value in p_tx

echo "Previous Values" >> $log
echo "p_rx: "$p_rx >> $log
echo "t_rx: "$p_tx >> $log

echo $rx > ~/scripts/idle/rx ## Write packets to RX file
echo $tx > ~/scripts/idle/tx ## Write packets to TX file

# Calculate threshold limit
t_rx=`expr $p_rx + 1000`
t_tx=`expr $p_tx + 1000`

echo "Threshold Values" >> $log
echo "t_rx: "$t_rx >> $log
echo "t_tx: "$t_tx >> $log
echo " " >> $log

if [ $rx -le $t_rx ] || [ $tx -le $t_tx ]; then ## If network packets have not changed that much
echo "Shutting down" >> $log
echo " " >> $log
rm ~/scripts/idle/rx
rm ~/scripts/idle/tx
sudo poweroff
fi
#Check if RX/TX Files Doesn't Exist
else
echo $rx > ~/scripts/idle/rx ## Write packets to file
echo $tx > ~/scripts/idle/tx
echo " " >> $log
fi

Finally I set up a cron job that calls the script regularly and then shuts down the device.


$ crontab -e

There I added the line:


*/20 * * * * sudo /usr/local/sbin/shutdown_if_inactive

which calls the script every 20 minutes.

13 Responses to Automatic shutdown after network inactivity

  1. Chaveiro says:

    Updated version:

    #!/bin/bash
    #
    # This is scheduled in CRON. It will run every 20 minutes
    # and check for inactivity. It compares the RX and TX packets
    # from 20 minutes ago to detect if they significantly increased.
    # If they haven’t, it will force the system to sleep.
    #

    dir=/tmp/shutdown_if_inactive
    if [ ! -e “$dir” ]; then
    mkdir “$dir”
    fi

    log=”$dir/log”

    # Extract the RX/TX packages
    rx=`/sbin/ifconfig enp1s0 | grep -m 1 RX | awk ‘{print $3}’`
    tx=`/sbin/ifconfig enp1s0 | grep -m 1 TX | awk ‘{print $3}’`

    #Write Date to log
    date >> $log
    echo “Current Values” >> $log
    echo “rx: “$rx >> $log
    echo “tx: “$tx >> $log

    # Check if RX/TX Files Exist
    if [ -f $dir/rx ] || [ -f $dir/tx ]; then
    p_rx=`cat $dir/rx` ## store previous rx value in p_rx
    p_tx=`cat $dir/tx` ## store previous tx value in p_tx

    echo “Previous Values” >> $log
    echo “p_rx: “$p_rx >> $log
    echo “t_rx: “$p_tx >> $log

    echo $rx > $dir/rx ## Write packets to RX file
    echo $tx > $dir/tx ## Write packets to TX file

    # Calculate threshold limit
    t_rx=`expr $p_rx + 1000`
    t_tx=`expr $p_tx + 1000`

    echo “Threshold Values” >> $log
    echo “t_rx: “$t_rx >> $log
    echo “t_tx: “$t_tx >> $log

    if [ $rx -le $t_rx ] || [ $tx -le $t_tx ]; then ## If network packets have not changed that much
    echo “Shutting down” >> $log
    echo ” ” >> $log
    rm $dir/rx
    rm $dir/tx
    sudo shutdown -t 30 Power off in 30sec due to network inactivity.
    fi

    #Check if RX/TX Files Doesn’t Exist
    else
    echo $rx > $dir/rx ## Write packets to file
    echo $tx > $dir/tx
    echo ” ” >> $log
    fi

  2. exxo says:

    I’ve put the script in /home folder and call them by crontab -e every 20 mins

    From what i’ve seen in the logfile the shutdown is called but the PC doesn’t shutdown.

    When i run the script manually it works.

    Do you have any suggestions what is going wrong here?

    Thank you.
    /exxo

  3. mergon says:

    I make some changes for better results

    1. I add the regex “Fehler” (thx Vannestra)
    # Extract the RX/TX
    rx=`/sbin/ifconfig eth0 | grep -m 1 RX | cut -d: -f2 | sed ‘s/ //g’ | sed ‘s/Fehler//g’ | sed ‘s/errors//g’`
    tx=`/sbin/ifconfig eth0 | grep -m 1 TX | cut -d: -f2 | sed ‘s/ //g’ | sed ‘s/Fehler//g’ | sed ‘s/errors//g’`

    2. I change all “~/scripts/*” to “/scripts/*” (thx Leander)

    3. I add the two lines:
    sudo rm /scripts/idle/rx
    sudo rm /scripts/idle/tx

    to “/etc/rc.local” it runs at the strart of the system. because the script run right is you make a hardreset or the user shut the system down or something other happens.

  4. Vannestra says:

    In a german ubuntu version you have to change the “error” in the regex with “Fehler” – its casesensitive!

  5. Hola! I’ve been reading your website for a while now and finally got the bravery to go ahead and give you a shout out from Huffman Tx!

    Just wanted to say keep up the good job!

  6. I’m impressed, I have to admit. Rarely do I encounter a blog that’s both educative and engaging, and without a doubt, you have hit the nail on the head. The issue is something that not enough people are speaking intelligently about. Now i’m very happy I came across this in my hunt for something relating to this.|

  7. Leander says:

    Hey there,

    thanks so much for your tutorials on this stuff!
    The script gets called and executes but the server does not shut down even if the network should be idle.
    I opened the log file and found an (unusually?) high rx value increase (about 10k each time)

    Here are my two latest entries:
    Thu Oct 18 17:20:01 CEST 2012
    Current Values
    rx: 82453
    tx: 280411

    Thu Oct 18 17:40:01 CEST 2012
    Current Values
    rx: 83050
    tx: 280443

    Do you have any idea what could be causing this?
    Or is just looking at the tx value maybe enough?

    Thanks!

    • cameacross says:

      Can’t you find out what causes that traffic? Any service running (e.g. some filesharing software or auto updates etc.)? If you don’t think there is running a process which could cause the rx traffic you could try your tx-only solution (if that fits your needs). Or you could adjust the threshold limit. But as i can see 83000 – 82500 = 500 so I think it should work because it is less than the 1000 limit.

      Does the script work if you call it manually (2 times with just a few seconds difference between)? If not, what is the output? Maybe you are having some permission problems (is the calling user allowed to call “poweroff”?

      • Leander says:

        The script did indeed not work properly because I require root privileges for the shutdown and ~/ as root did not point to anything useful as I do not have a separate root account. I exchanged ~/ with my actual home directory and added the job with sudo crontab -e

        The computer now shut down after 20 minutes of inactivity. Guess there was something running on the network when I first tried it.

  8. Martin says:

    Thanks. Is working fine after adjusting to my NAS

  9. Fletch says:

    Interesting, how do you power the NAS on again?

Leave a reply to cameacross Cancel reply