Skip to content

Instantly share code, notes, and snippets.

@tuklusan
Created September 7, 2021 22:49
Show Gist options
  • Save tuklusan/a13de0f705dd66437b77a5d502635d8f to your computer and use it in GitHub Desktop.
Save tuklusan/a13de0f705dd66437b77a5d502635d8f to your computer and use it in GitHub Desktop.
SANYALnet Labs | Save State of VirtualBox Virtual Machines on APC UPS power loss shutdown by apcupsd Linux Daemon / onbattery event handler script: see https://supratim-sanyal.blogspot.com/2021/09/save-state-of-virtualbox-virtual.html
#!/bin/bash
#
# /etc/apcupsd/onbattery
#
# This shell script if placed in /etc/apcupsd
# will be called by /etc/apcupsd/apccontrol when the UPS
# goes on batteries.
# We send an email message to root to notify him.
#
# On AC power loss, the events raised seem to be:
# 2021-08-17 11:13:34 +0000 Power failure.
# 2021-08-17 11:13:40 +0000 Running on UPS batteries.
# 2021-08-17 11:13:43 +0000 Reached remaining time percentage limit on batteries.
# 2021-08-17 11:13:43 +0000 Initiating system shutdown!
# apccontrol seems to fork off the scripts for later events even when prior events
# are still being handled. So all other event handlers are coded to return 99
# to abort, so that only this one processes the "on battery" event completely
# and shuts down
logger "$0 $(date): START"
wall "$0 $(date): START"
echo "$0 $(date): START"
#HOSTNAME=`hostname`
#MSG="$HOSTNAME UPS $1 Power Failure !!!"
#
#(
# echo "$MSG"
# echo " "
# /sbin/apcaccess status
#) | $APCUPSD_MAIL -s "$MSG" $SYSADMIN
# Reclaim cache memory
time /root/clear-kernel-memory-cache.sh > /var/log/clear-kernel-memory-cache.shutdown.log 2>&1
time /root/save-vms-on-ups-power.sh > /var/log/save-vms-on-ups-power.shutdown.log 2>&1
sync;sync;sync
poweroff
# Exit 0 for apcupsd to continue to shutdown; 99 to abort shutdown
logger "$0 $(date): END"
wall "$0 $(date): END"
echo "$0 $(date): END"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment