How it works Link to heading

It measures an in/outbound network traffic and logs in given intervals. If the amount of data sent from the server don’t exceeds a set limit, the server suspends itself. It’s a very useful script intended to use in home servers that are not meant to be turned on constantly.

You can ‘wake’ your home server using wakeonlan tool by typing:

wakeonlan [server's MAC address]

And after the transmission (video streaming, file syncynig, torrent download) is ended it will suspend itself.

Setup Link to heading

On Debian-based system you need to add a rule to sudoers file (sudo visudo):

user ALL=(ALL) NOPASSWD: /usr/sbin/pm-suspend

to be able to execute te script correctly. Then setup a cron job like this:

crontab -e
*/15 **** sh /path/to/autosleep.sh

Drawbacks Link to heading

  1. Sometimes, especially after heavy downloading, it takes two cycles for the server to reach the set limit and go to sleep - so in my case, 30 minutes instead of 15.
  2. If you set a torrent download and leave it, remember that even after the downloading is completed, it’s still uploading. So unless you disable upload, the server won’t suspend because it’s still receiving / transmitting packages.

The Script Link to heading

#!/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=/home/user/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 /home/user/scripts/idle/rx ] || [ -f /home/user/scripts/idle/tx ]; then
	p_rx=`cat /home/user/scripts/idle/rx`  ## store previous rx value in p_rx
	p_tx=`cat /home/user/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 > /home/user/scripts/idle/rx    ## Write packets to RX file
	echo $tx > /home/user/scripts/idle/tx    ## Write packets to TX file
	
	# Calculate threshold limit 
	t_rx=`expr $p_rx + 3000`
	t_tx=`expr $p_tx + 10000`

	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 "Suspend to Ram ..." >> $log
		echo " " >> $log
		rm /home/user/scripts/idle/rx
		rm /home/user/scripts/idle/tx
		sudo pm-suspend  ## Force Sleep
	fi
	
#Check if RX/TX Files Doesn't Exist
else 
	echo $rx > /home/user/scripts/idle/rx ## Write packets to file
	echo $tx > /home/user/scripts/idle/tx
	echo " " >> $log
fi 

License & Download Link to heading

This is just a modified version of a script I found in Ubuntu Forums, originally posted by the user vashwood. I claim no copyright to it.