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.
[bg_collapse view=“link” expand_text=“Show More” collapse_text=“Show Less”] [code lang=“bash”]
#!/bin/bash mac_address=bc:5f:f4:00:00:00 interface=internal serverip=192.168.0.1
ping -c1 $serverip
if [[ $? != 0 ]]
then /usr/sbin/ether-wake -i $interface $mac_address fi
[/code] [/bg_collapse]
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
fedora-kde :: ~ » sudo dnf install ethtool
// check Wake-on-Lan status
fedora-kde :: ~ » 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.
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.
[bg_collapse view=“link” expand_text=“Show More” collapse_text=“Show Less”] [code lang=“bash”] #!/bin/bash
# Function 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 VM=($(virsh list –name | grep file -v)) #exclude fileserver
kodi_ip=“IP ADDRESS” kodi_user=root
desktop_ip=“IP ADDRESS” desktop_user=“YOUR USERNAME”
gotify_server_url=“YOU 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 ## This is for the notification of the user 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 shuting 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 shuting down in 5min’”
fi
echo “Server preparing for Shutdown. Waiting for 5min” | adddate » $shutdown_log # Wait for 5 min after the notification has been send out sleep 300
# While Array is not empty do “shutdown domain” 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
# Add Info to logfile #virsh shutdown file | adddate » $shutdown_log
## Shutdown the physical server sleep 60 echo “Server Shutdown /n” | adddate » $shutdown_log /bin/systemctl poweroff » $shutdown_log
[/code] [/bg_collapse]
Kommentare