From 04db5027c156f23eb59f825ccdd7bb47ef8d1625 Mon Sep 17 00:00:00 2001 From: Dunestorm Date: Sat, 24 Sep 2022 17:44:09 +0100 Subject: [PATCH] [Proxmox Timed Shutdown] - Log file function with date/time stamp prefix. - Log file rotation. - Shutdown loop to handle overrunning backup tasks. --- ProxmoxTimedShutdown.sh | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/ProxmoxTimedShutdown.sh b/ProxmoxTimedShutdown.sh index 2421a50..a3bc431 100644 --- a/ProxmoxTimedShutdown.sh +++ b/ProxmoxTimedShutdown.sh @@ -1,12 +1,36 @@ #!/bin/bash -echo "Waiting 1hr to allow user to cancel shutdown" +LOGFILE="/var/log/timedshutdown.log" +touch $LOGFILE + +# Log file ################################################################# +log () { + TIMEDATE=$(date +%Y/%m/%d_%H:%M:%S) + echo "$TIMEDATE $1" >> $LOGFILE +} + +if [ `cat $LOGFILE | wc -l` -ge 100 ]; then + >$LOGFILE + log "Log file was rotated" +fi +############################################################################ + +log "Waiting 1hr to allow user to cancel shutdown" sleep 1h -if [ `ps -ef | grep vzdump | wc -l` -eq 1 ]; then - echo "Triggering scheduled post-backup shutdown" - /usr/sbin/shutdown -h now -else - echo "Automatic shutdown not triggered due to concurrent backup task" - exit 2 -fi \ No newline at end of file +# Main loop ################################################################ +for i in 1 2 3 4 +do + if [ `ps -ef | grep vzdump | wc -l` -eq 1 ]; then + log "Triggering scheduled post-backup shutdown" + /usr/sbin/shutdown -h now + exit 1 + else + log "Automatic shutdown delayed due to concurrent backup task" + sleep 15m + fi +done + +log "Automatic shutdown cancelled due to concurrent backup task" +exit 2 +############################################################################ \ No newline at end of file