Skip to content

Instantly share code, notes, and snippets.

@jcconnell
Created January 8, 2021 07:39
Show Gist options
  • Save jcconnell/d39aa88b43f56698dc4e5a12b7724d8c to your computer and use it in GitHub Desktop.
Save jcconnell/d39aa88b43f56698dc4e5a12b7724d8c to your computer and use it in GitHub Desktop.
Bash scripts to start / stop Proxmox containers and VMs
#!/bin/bash
# Script to suspend and resume container
# (c) July 2020 JC Connell
# if you want to see your debug messages, uncomment this variable
#DEBUG_IS_ON=yes
# you can use this function for debug messages
# usage: command: "debug File Is Open" --(gives oputput)--> "At 11:44:00 File Is Open"
function debug()
{
if [ "${DEBUG_IS_ON}" = "yes" ]
then
NOW=$(date +"%T")
echo "At $NOW Debug: ${*}" >&2
fi
}
function print_help()
{
echo -e "Syntax: $0 [-n] [-s] [-r] [-c] >&2"
echo -e "\t[-n]: Container ID (Required)"
echo -e "\t[-s]: Suspend container"
echo -e "\t[-r]: Resume container"
echo -e "\t[-c]: Check container status"
exit 1
}
# Suspend container
function process_suspend()
{
debug Processing 'suspend'
status=$(process_check)
if [ "$status" == "1" ]; then
debug "Suspending container"
/usr/sbin/pct shutdown "$id"
elif [ "$status" == "0" ]; then
debug "Container not running"
fi
}
# Resume container
function process_resume()
{
debug Processing 'resume'
status=$(process_check)
if [ "$status" == "0" ]; then
debug "Resuming container"
/usr/sbin/pct start "$id"
elif [ "$status" == "1" ]; then
debug "Container already running"
fi
}
# Check container status
function process_check()
{
debug Procesing 'check'
status=$(/usr/sbin/pct status $id)
if [ "$status" == "status: stopped" ]; then
echo 0
elif [ "$status" == "status: running" ]; then
echo 1
else
debug "Error checking container status"
fi
}
while getopts ":srchn:" o
do
case "${o}" in
s) process_suspend ;;
r) process_resume ;;
c) process_check ;;
h) print_help ;;
n) id=${OPTARG} ;;
*) print_help ;;
esac
done;
#!/bin/bash
# Script to suspend and resume vm
# (c) July 2020 JC Connell
# if you want to see your debug messages, uncomment this variable
#DEBUG_IS_ON=yes
# you can use this function for debug messages
# usage: command: "debug File Is Open" --(gives oputput)--> "At 11:44:00 File Is Open"
function debug()
{
if [ "${DEBUG_IS_ON}" = "yes" ]
then
NOW=$(date +"%T")
echo "At $NOW Debug: ${*}" >&2
fi
}
function print_help()
{
echo -e "Syntax: $0 [-n] [-s] [-r] [-c] >&2"
echo -e "\t[-n]: VM ID (Required)"
echo -e "\t[-s]: Suspend VM"
echo -e "\t[-r]: Resume VM"
echo -e "\t[-c]: Check VM status"
exit 1
}
# Suspend vm
function process_suspend()
{
debug Processing 'suspend'
status=$(process_check)
if [ "$status" == "1" ]; then
debug "Suspending container"
/usr/sbin/qm shutdown "$id"
elif [ "$status" == "0" ]; then
debug "Container not running"
fi
}
# Resume vm
function process_resume()
{
debug Processing 'resume'
status=$(process_check)
if [ "$status" == "0" ]; then
debug "Resuming container"
/usr/sbin/qm start "$id"
elif [ "$status" == "1" ]; then
debug "Container already running"
fi
}
# Check vm status
function process_check()
{
debug Procesing 'check'
status=$(/usr/sbin/qm status $id)
if [ "$status" == "status: stopped" ]; then
echo 0
elif [ "$status" == "status: running" ]; then
echo 1
else
debug "Error checking container status"
fi
}
while getopts ":srchn:" o
do
case "${o}" in
s) process_suspend ;;
r) process_resume ;;
c) process_check ;;
h) print_help ;;
n) id=${OPTARG} ;;
*) print_help ;;
esac
done;
# start container 200 at 6AM M-F
0 6 * * 1-5 /root/scripts/auto-resume-shutdown-container.sh -n 200 -r >/dev/null 2>&1
# stop container 200 at 6PM M-F
0 18 * * 1-5 /root/scripts/auto-resume-shutdown-container.sh -n 200 -s >/dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment