Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for a way to put my Linux home server in standby after a period without any "heart beat" network activity. I would like to use WOL packets as a heart beat signal. These packages will be send by several clients each with an interval of, lets say, every 15 minutes.

I am quite a rookie concerning Linux scripting, but I can understand code after some studying. An actual example is therefore more than welcome.

share|improve this question

migrated from serverfault.com Oct 26 '12 at 12:59

This question came from our site for system and network administrators.

Why not just disable standby on the server? In my world, servers and standby should never be in the same sentence, unless people tells you not to do it ;) – Frederik Nielsen Oct 26 '12 at 10:41
Because I do not want my home server to be powered on the whole day. But only when PC's are powered on, or smartphones te be in WiFi range. – Alain Oct 26 '12 at 11:23
Have you tried just pinging the server? I don't know if that is enough to make it not go to sleep. – Frederik Nielsen Oct 26 '12 at 12:37
Thanks for you suggestion. Currently the server does not go into standby by itself. The solution that I am looking for should take care of this as well. I am planning to add service monitoring, to prevent the server going to sleep while, for example, a backup process is running. But first things first. ;-) – Alain Oct 26 '12 at 12:49
@ChrisS "Modern hardware 'wears' the…" Well, sort of. A power/heat cycle does induce a certain amount of wear. But so does running it. That power cycle is the same as running it for X time. So, if X is less than the amount of time you had it not running, you're prolonging life. If X is greater, you're shortening life. I doubt X is more than a few hours, even for the worst-case component. (A lot of components are expected to have rapid heat cycling, e.g, the CPU) – derobert Oct 26 '12 at 15:48

Here are a couple of easy ways for your server to check if other devices are using it:

  • Check the arp cache using /usr/sbin/arp or by reading /proc/net/arp. You'll see in there all the devices which the server has communicated with fairly recently. This includes the MAC address, so you can find your phone, etc. even in the face of DHCP.
  • Use ping (or arping, which won't be blocked by host-based firewalls) to actively poll your hosts.
  • Set up iptables rules to match the hosts you're interested in, then check their packet counters. If the counters are increasing, those hosts are active.

All of those are doable with fairly simple scripting. You can then have the script put your server in S3, S4, or even G2/S5 and let Wake-on-LAN wake the server back up.

NOTE: On a lot of desktop boards, S3 doesn't actually save that much power. Meter it before you bother. Or make sure to use at least S4.

share|improve this answer
  shutdown -c
  shutdown -h +15 &
  echo "Waiting for magic packet to continue ..."
  nc -l 9

As requested, here's the missing documentaion. Late, but maybe still helpful:

The loop cancels the previous shutdown command and sets a new one to 15 minutes. Then it opens port 9 with NetCat utility. If a packet on this port is received, the loop will start over, otherwise the computer will shut down.

share|improve this answer
Can you add some detail to this script? – slm Jan 22 '14 at 17:39
This script should work, but I concur with @slm -- the purpose of this site is to educate as much as it is to answer; explaining how this script works and why it solves the stated problem would improve this answer. – Shadur Jan 22 '14 at 18:00

The following code will shutdown the computer if no WOL packets were sent within a 15 minute timeframe.

TIME_LIMIT=$(( 15 * 60 )) # 15 minutes

if [[ $(whoami) != 'root' ]] ; then
   echo "ERROR: this script requires root privilages" >&2
   exit 9

set -m
while true ; do
   #Run command and save PID
   tcpdump -q -c 1 -i eth0 -nn udp port 9 >/dev/null 2>&1  &

   #Fork off timer
   bash -c "sleep $TIME_LIMIT ; kill -0 $TCPPID 2>/dev/null && kill -9 $TCPPID " &

   wait $TCPPID
   if [[ $? -ne 0 ]] ; then
      # no one connected - cya!
      shutdown -Ph now
      exit 1
      #We found something - now to wait for the rest of the 15 minutes
      fg %2
share|improve this answer

You can put your server into sleep with s2disk and s2ram. The question when? is up to you. You may run some cron jobs and ping some IP's when they do not answer, then go to sleep. Or what ever.

With tools like etherwake or wakeonlan you are able to wake a machine from sleep by sending a magic packet to network.

Good reads Wake-on-LAN

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.