Put Linux server into sleep and wake it up using WOL packets


Solution 1

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.

Solution 2

  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.


Related videos on Youtube

Rui F Ribeiro
Author by

Rui F Ribeiro

Updated on July 19, 2022


  • Rui F Ribeiro
    Rui F Ribeiro 1 day

    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.

    An actual example is therefore more than welcome.

    • Admin
      Admin over 9 years
      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 ;)
    • Admin
      Admin over 9 years
      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.
    • Admin
      Admin over 9 years
      Have you tried just pinging the server? I don't know if that is enough to make it not go to sleep.
    • Admin
      Admin over 9 years
      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. ;-)
    • Admin
      Admin over 9 years
      Couple quick thoughts: 1. Modern hardware "wears" the most from heat cycles caused by turning things on/off. Turning your server on/off constantly will cut it's average life by half or more. 2. Electricity is cheap in most places, you should figure out the actual amount you'd be saving by doing this (I'll bet it's <$3/month, unless you have a horribly inefficient server, in which case you might want to consider using more power-efficient hardware). 3. It's wouldn't be too hard to program a watchdog service that accepts UDP packets to reset it's counter and sleeps the computer when tripped.
    • Admin
      Admin over 9 years
      @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)
    • Admin
      Admin over 9 years
      My recommendation would be to run a low-powered ARM processor, and forget about power consumption, which would be negligible when the processor is idle. It does currently restrict your choices for storage, unfortunately.
  • slm
    slm over 8 years
    Can you add some detail to this script?
  • Shadur
    Shadur over 8 years
    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.