05. November 2021 • 3 min. read

Simple Wake on Lan Script

Linux

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.

#!/bin/bash

mac_address=bc:5f:f4:00:00:00
interface=internal
serverip=192.168.0.1

ping -c 1 "$serverip"
if [[ $? != 0 ]]; then
    /usr/sbin/ether-wake -i "$interface" "$mac_address"
fi

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/kvm-server-start.sh

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.

// Install ethtool
$ sudo dnf install ethtool

// check Wake-on-Lan status
$ sudo dnf ethtool eth0 | grep Wake-on
        Supports Wake-on: pumbg
        Wake-on: g

// To make it permanent, add this entry to the ifcfg file.
$ 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.

#!/bin/bash

# ─── Functions ────────────────────────────────────────────────────────────────

adddate() {
    while IFS= read -r line; do
        echo "$(date "+%b %d %R:%S") $(hostname -s) Server-Shutdown: $line"
    done
}

# ─── Variables ────────────────────────────────────────────────────────────────

retry=0
zahl=0

# Write virt domains into array (exclude fileserver)
VM=($(virsh list --name | grep -v file))

kodi_ip="IP ADDRESS"
kodi_user=root

desktop_ip="IP ADDRESS"
desktop_user="YOUR USERNAME"

gotify_server_url="YOUR URL"
gotify_server_token="YOUR TOKEN"
gotify_title="KVM Server Shutdown"
gotify_message="Server shutdown in 5 min"
gotify_priority="5"

shutdown_log=/var/log/shutdown/shutdown-script.log

# ─── Execution ────────────────────────────────────────────────────────────────

## Notify users

ping "$kodi_ip" -c 1
if [[ $? == 0 ]]; then
    # Notify Kodi
    ssh "$kodi_user@$kodi_ip" -i ~/.ssh/notify \
        "kodi-send -a 'Notification(Shutdown,KVM Server is shutting down in 5min)'"
fi

ping "$desktop_ip" -c 1
if [[ $? == 0 ]]; then
    # Notify Desktop
    ssh "$desktop_user@$desktop_ip" -i ~/.ssh/notify \
        "notify-send -i /home/user/Pictures/shutdown.svg 'KVM Server is shutting down in 5min'"
fi

echo "Server preparing for Shutdown. Waiting for 5min" | adddate >> "$shutdown_log"

# Wait 5 minutes after notification
sleep 300

## Shut down virtual machines

echo "VM shutdown" | adddate >> "$shutdown_log"

while [[ -n ${VM[$zahl]} ]]; do
    echo "${VM[$zahl]}" | adddate >> "$shutdown_log"
    virsh shutdown "${VM[$zahl]}" | adddate >> "$shutdown_log"
    zahl=$(expr $zahl + 1)
done

## Shut down physical server

sleep 60
echo "Server Shutdown" | adddate >> "$shutdown_log"
/bin/systemctl poweroff >> "$shutdown_log"

Comments

Search