save electricity
source / license: behance.net

A simple bash script to automatically suspend idle home server to save electricity

22/03/2015

How it works

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

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

  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.

License & Download

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.

You can download the latest version of the script from my Github repo.