Simple Wake on Lan Script

  • Post author:
  • Reading time:6 mins read
  • Post category:Bash / Linux
  • Post comments:0 Comments

A short one today.

In my last post, I mentioned my “home server”. That thing actually does not run 24/7, even though it’s a “server” (electricity is quite expensive here). There is no point in it running when I am not home and/or it only idles. But I also don’t want to walk up to it to power on the server, every time I want to access something. So I created a very simple script to boot it via WoL, which runs automatically on Mondays and Fridays around 18:00 / 6 pm, primarily for the backup jobs.

The script checks if the server is alive via a ping. If it’s not running, it executes the “ether-wake” command.

I configured it as a cronjob on my “router”, a Zotac CI323 nano. This mini PC is great because of its two ethernet ports, which makes it almost perfect for a router. I installed Rocky Linux 8 with KVM on it. On top of that, I am running “OPNSense”, “Nextcloud”, “Pihole” and “Paperless” as virtual systems.

Anyway, the cronjob looks like this.

#Ansible: Start KVM Server for Backup
45 17 * * MON,FRI /home/admin/script/

Keep in mind that your system has to support WoL and you have to enable it in the BIOS. Check your mainboard manual (most systems should support it). On my old system, the following step was not necessary, but for some reason on this one, I also need to enable WoL in Linux. I guess the system keeps resetting the setting on every boot.

fedora-kde :: ~ » sudo dnf install ethtool

fedora-kde :: ~ » sudo dnf ethtool eth0 | grep Wake-on
        Supports Wake-on: pumbg
        Wake-on: g

fedora-kde :: ~ » sudo vim /etc/sysconfig/network-scripts/ifcfg-eth0
  ETHTOOL_OPTS="wol g"

I also created a script to shut the server down. It’s not pretty, but it works. Since I am running a bunch of VMs on it, I wanted to make sure that they shut down properly before the host powers off. This script runs every weekday at around 22:00 / 10 pm and at midnight on the weekends.

This script is very specific to my system, very old and not exactly good code (should be at least 5 years now). But maybe someone will find it useful/interesting.

Leave a Reply