Files
Varaverk/unRAID Essentials/server-reboot.sh
T
2026-03-27 16:50:20 +00:00

152 lines
4.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
set -e
# ----------------------------------------------------------------------------------------------
# ---------------------------- Unattended Reboot Script for Unraid -----------------------------
# ----------------------------------------------------------------------------------------------
# ---------------- User Variables, Please adjust in Master.conf as needed ----------------------
# ----------------------------------------------------------------------------------------------
#
# User variables can be adjusted in Master.conf
#
# Scripts now just contain runtime logic
#
# This new setup allows for profiles and arguments to use the core script
# Use arguments in unRAID User Plugin for directory
# Example: /mnt/user/appdata/unraid_scripts/Rsync/rsync.sh --dry-run
# ----------------------------------------------------------------------------------------------
# -------------- End Of User Variables, Please adjust in Master.conf as needed -----------------
# ----------------------------------------------------------------------------------------------
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../Master.conf"
source "$SCRIPT_DIR/../common.sh"
parse_args "$@"
# STATUS MODE
if [[ "$SHOW_STATUS" == true ]]; then
echo
echo "=================================================="
echo " ️ REBOOT STATUS"
echo "--------------------------------------------------"
echo " ⏱ Delay: ${REBOOT_SLEEP:-0}s"
echo " 🧪 Dry Run: $DRY_RUN"
echo "=================================================="
exit 0
fi
# CONFIG
validate_int REBOOT_SLEEP "$REBOOT_SLEEP"
# HEADER
echo
echo "============================================================"
echo " 🚀 UNRAID REBOOT SEQUENCE STARTING"
echo "------------------------------------------------------------"
echo " ⏱ Delay: ${REBOOT_SLEEP}s"
echo " 🧪 Dry Run: $DRY_RUN"
echo "============================================================"
log "️ Reboot sequence initiated"
# FUNCTIONS
notify_users() {
warn "🟡 Notifying users of reboot"
wall "⚠️ Unraid server will reboot in ${REBOOT_SLEEP} second(s). Save your work."
}
stop_docker() {
if [[ "$DRY_RUN" == true ]]; then
warn "🧪 Would stop Docker service"
return
fi
info "🟡 Stopping Docker service"
if /etc/rc.d/rc.docker stop; then
info "🟢 Docker stopped"
else
warn "🔴 Docker stop failed or already stopped"
fi
}
stop_vm_manager() {
if [[ "$DRY_RUN" == true ]]; then
warn "🧪 Would stop VM Manager (libvirt)"
return
fi
info "🟡 Stopping VM Manager"
if /etc/rc.d/rc.libvirt stop; then
info "🟢 VM Manager stopped"
else
warn "🔴 VM Manager stop failed or already stopped"
fi
}
sync_disks() {
if [[ "$DRY_RUN" == true ]]; then
warn "🧪 Would sync filesystem buffers"
return
fi
info "🟡 Syncing disks"
if sync; then
info "🟢 Disk sync complete"
else
warn "🔴 Sync command returned error"
fi
}
reboot_system() {
if [[ "$DRY_RUN" == true ]]; then
warn "🧪 Would reboot system (/sbin/reboot)"
return
fi
info "🔴 Rebooting system NOW"
/sbin/reboot
}
# EXECUTION
START_TIME=$(date +%s)
if [[ "$REBOOT_SLEEP" -gt 0 ]]; then
notify_users
info "⏱ Waiting ${REBOOT_SLEEP}s before shutdown sequence"
sleep "$REBOOT_SLEEP"
fi
stop_docker
stop_vm_manager
sync_disks
reboot_system
# NOTE: system will not reach here unless dry-run
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
# SUMMARY
echo
echo "============================================================"
if [[ "$DRY_RUN" == true ]]; then
echo " 🟡 REBOOT SUMMARY"
echo "------------------------------------------------------------"
echo " ⏱ Delay: ${REBOOT_SLEEP}s"
echo " 📡 Status: 🟡 DRY RUN (no reboot executed)"
else
echo " 🔴 REBOOT SUMMARY"
echo "------------------------------------------------------------"
echo " ⚠️ Status: 🔴 SYSTEM SHOULD BE REBOOTING"
echo " ⏱ Duration: ${DURATION}s"
fi
echo "============================================================"
info "️ Reboot sequence complete (or handed off to system reboot)"