jpprojects

Proxmox

Proxmox Downtime Scripting

Personal servers (probably) do not need to be on all the time. By scripting downtime at night the server uses less electricity and other resources. This page details configurations for automated down-scrits and up-scripts.

Down Scripting

A script executed regularly via cron allows for logs to track the down-timing. Some configurations in the Proxmox UI, as well as in individual VMs.

1. Enable QEMU Guest Agent

QEMU gues agent is a lightweight service inside a VM that (among other things) allows the hypervisor (in our case, Proxmox) to communicate directly with the guest OS. It’s a safe way to interact with a VM, particularly for powering on and off.

By defauly, Proxmox disables Qemu guest agent for each VM and must be enabled per VM.

2. Install the QEMU Guest Agent

These steps need to be run on every VM.

Enter the terminal of the VM itself. Follow these commands to install.

3. Create the script

In the terminal of the Proxmox host, create the script with nano safe-shutdown.sh:

#!/bin/bash

LOGFILE="/var/log/proxmox-safe-shutdown.log"
DATE="$(date '+%Y-%m-%d %H:%M:%S')"

echo "========================================" >> "$LOGFILE"
echo "$DATE : Starting safe Proxmox shutdown" >> "$LOGFILE"

# Get list of running VMs
RUNNING_VMS=$(qm list | awk 'NR>1 && $3=="running" {print $1}')

if [ -z "$RUNNING_VMS" ]; then
    echo "$DATE : No running VMs found" >> "$LOGFILE"
else
    for VMID in $RUNNING_VMS; do
        VMNAME=$(qm config "$VMID" | grep '^name:' | awk '{print $2}')
        echo "$DATE : Sending shutdown to VM $VMID ($VMNAME)" >> "$LOGFILE"
        qm shutdown "$VMID"
    done
fi

# Wait for VMs to stop
echo "$DATE : Waiting for VMs to shut down..." >> "$LOGFILE"

while true; do
    STILL_RUNNING=$(qm list | awk 'NR>1 && $3=="running" {print $1}')
    if [ -z "$STILL_RUNNING" ]; then
        echo "$(date '+%Y-%m-%d %H:%M:%S') : All VMs are stopped" >> "$LOGFILE"
        break
    fi
    sleep 10
done

echo "$(date '+%Y-%m-%d %H:%M:%S') : Powering off host" >> "$LOGFILE"
/sbin/poweroff

If you ssh into the Proxmox host you can copy and paste this whole script in. I have not had luck doing that in the Proxmox UI terminal.

This script sends a safe ACPI shutdown signal to each active VM, then turns off the Proxmox host itself, and logs the whole process. The log is accessible at /var/log/proxmox-safe-shutdown.log.

4. Set up the cronjob

5. Test the Job