Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save TheHarold/e680f0061a47562cc92332758484b098 to your computer and use it in GitHub Desktop.
Save TheHarold/e680f0061a47562cc92332758484b098 to your computer and use it in GitHub Desktop.
Shutdown proxmox after 30 mins of being idle

Background
I've a home lab running Proxmox and I often forget to shut the host, wasting power even when there's no containers or virtual machines running. This script is setup on cron to run every 5 mins, it checks, if there are no containers & Virtual machines running then it will shut down the host in 30 mins or 1800 sec.

#! /bin/bash

if [[ $(/usr/sbin/pct list | /usr/bin/grep -i running | /usr/bin/wc -l) == 0 && $(/usr/sbin/qm list | /usr/bin/grep running | /usr/bin/wc -l) == 0 ]]; then
    /usr/bin/sleep 1800
	if [[ $(/usr/sbin/pct list | /usr/bin/grep -i running | /usr/bin/wc -l) == 0 && $(/usr/sbin/qm list | /usr/bin/grep running | /usr/bin/wc -l) == 0 ]]; then
		/usr/bin/init 0
	fi
else
    /usr/bin/echo "Resources in use"
fi

To setup chron, copy paste the following into your chrontab file.

# Shutdown idle proxmox automatically after 30 mins
*/5 * * * * pidof -x shut && echo "already running" >> /dev/null 2>&1 || /root/shut.sh
@docop
Copy link

docop commented Jun 19, 2023

Hi this is a nice find to close the server. But how can we change the script if like 2 vm.. used as only router .. are on all the time ?
thanks in advance

@TheHarold
Copy link
Author

Hi this is a nice find to close the server. But how can we change the script if like 2 vm.. used as only router .. are on all the time ? thanks in advance

If I understood, you want to shut the host proxmox if you have nothing running except your possibly virtual firewall which you are calling as router?

@docop
Copy link

docop commented Jun 19, 2023

yes , proper router, just like RouterOs can be in vm or pfsense... so how can i shutdown while having 2 vm running with your script ?

@TheHarold
Copy link
Author

You may try the below, I've not tested it though

#!/bin/bash

FIREWALL="firewall-container"
ROUTER="virtual-router"

if [[ $(/usr/sbin/pct list | /usr/bin/grep -i running | /usr/bin/wc -l) == 0 && $(/usr/sbin/qm list | /usr/bin/grep running | /usr/bin/wc -l) == 0 ]]; then
    /usr/bin/sleep 1800

    if [[ $(/usr/sbin/pct list | /usr/bin/grep -i running | /usr/bin/wc -l) == 0 && $(/usr/sbin/qm list | /usr/bin/grep running | /usr/bin/wc -l) == 0 ]]; then
        /usr/bin/init 0
    fi
else
    exceptions_running=0

    if /usr/sbin/pct list | /usr/bin/grep -iq "$FIREWALL" && /usr/sbin/pct list | /usr/bin/grep -iq "$FIREWALL running"; then
        /usr/bin/echo "Firewall '$FIREWALL' is running. Skipping shutdown..."
        exceptions_running=1
    fi

    if /usr/sbin/qm list | /usr/bin/grep -iq "$ROUTER" && /usr/sbin/qm list | /usr/bin/grep -iq "$ROUTER running"; then
        /usr/bin/echo "Virtual router '$ROUTER' is running. Skipping shutdown..."
        exceptions_running=1
    fi

    if [[ $exceptions_running -eq 0 ]]; then
        /usr/bin/sleep 1800

        if [[ $(/usr/sbin/pct list | /usr/bin/grep -i running | /usr/bin/wc -l) == 0 && $(/usr/sbin/qm list | /usr/bin/grep running | /usr/bin/wc -l) == 0 ]]; then
            /usr/bin/init 0
        fi
    fi
fi

@docop
Copy link

docop commented Jun 24, 2023

oh thanks.. as i will install prox 8, i will try out directly. I now see the lxv and vm separation clearly.
At same time, is the < == 0 > mean it return 0 lxc running ? Like if i change to : == 1 , will it be it will close anyway if 1 or 0 lxc are running ?

Thanks again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment