From d8364023486f1a8b8eed6bfe7a15566cf9f14ab5 Mon Sep 17 00:00:00 2001 From: "CanbiZ (MickLesk)" <47820557+MickLesk@users.noreply.github.com> Date: Thu, 26 Mar 2026 13:45:02 +0100 Subject: [PATCH] Add status/run/rotate options & improve updater tools/pve/cron-update-lxcs.sh: add show_status, run_now and rotate_log functions; expose new menu options (Status, Run, Rotate) and enlarge menu. Consolidate license line formatting. tools/pve/update-lxcs-cron.sh: tighten variable scoping, improve ostype detection and logging, add fallback for hostname, handle unknown OS types, harden apt workflow (retry with By-Hash workaround and refresh lists), skip template containers correctly, add error reporting on container updates, and use a timed shutdown. Update author/license header. Overall: adds manual control and status/log rotation to the cron manager and makes the container updater more robust and safer for Debian/Ubuntu-based containers. --- tools/pve/cron-update-lxcs.sh | 74 +++++++++++++++++++++++++++++++++-- tools/pve/update-lxcs-cron.sh | 36 +++++++++++------ 2 files changed, 95 insertions(+), 15 deletions(-) diff --git a/tools/pve/cron-update-lxcs.sh b/tools/pve/cron-update-lxcs.sh index 636f8ed7..158e4eca 100644 --- a/tools/pve/cron-update-lxcs.sh +++ b/tools/pve/cron-update-lxcs.sh @@ -2,8 +2,7 @@ # Copyright (c) 2021-2026 community-scripts ORG # Author: MickLesk (CanbiZ) -# License: MIT -# https://github.com/community-scripts/ProxmoxVED/raw/main/LICENSE +# License: MIT | https://github.com/community-scripts/ProxmoxVED/raw/main/LICENSE # # This script manages a local cron job for automatic LXC container OS updates. # The update script is downloaded once, displayed for review, and installed @@ -191,19 +190,88 @@ view_script() { echo "" } +show_status() { + echo "" + if [[ -f "$LOCAL_SCRIPT" ]]; then + local hash + hash=$(sha256sum "$LOCAL_SCRIPT" | awk '{print $1}') + ok "Script installed: ${LOCAL_SCRIPT}" + echo -e " \e[36mSHA256:\e[0m ${hash}" + echo -e " \e[36mInstalled:\e[0m $(stat -c '%y' "$LOCAL_SCRIPT" 2>/dev/null | cut -d. -f1)" + else + err "Script not installed" + fi + + if crontab -l -u root 2>/dev/null | grep -q "${LOCAL_SCRIPT}"; then + local schedule + schedule=$(crontab -l -u root 2>/dev/null | grep "${LOCAL_SCRIPT}" | awk '{print $1,$2,$3,$4,$5}') + ok "Cron active: ${schedule}" + else + err "Cron not configured" + fi + + if [[ -f "$CONF_FILE" ]]; then + local excludes + excludes=$(grep -oP '^\s*EXCLUDE\s*=\s*\K.*' "$CONF_FILE" 2>/dev/null || echo "(none)") + echo -e " \e[36mExcluded:\e[0m ${excludes:-"(none)"}" + fi + + if [[ -f "$LOG_FILE" ]]; then + local log_size last_run + log_size=$(du -h "$LOG_FILE" | awk '{print $1}') + last_run=$(grep -oP '^\s+\K\w.*' "$LOG_FILE" | tail -1) + echo -e " \e[36mLog file:\e[0m ${LOG_FILE} (${log_size})" + [[ -n "${last_run:-}" ]] && echo -e " \e[36mLast run:\e[0m ${last_run}" + else + echo -e " \e[36mLog file:\e[0m (no runs yet)" + fi + echo "" +} + +run_now() { + if [[ ! -f "$LOCAL_SCRIPT" ]]; then + err "No local script found at ${LOCAL_SCRIPT}. Use 'Add' first." + exit 1 + fi + info "Running update script now..." + bash "$LOCAL_SCRIPT" | tee -a "$LOG_FILE" + ok "Run completed. Log appended to ${LOG_FILE}" +} + +rotate_log() { + if [[ ! -f "$LOG_FILE" ]]; then + info "No log file to rotate." + return + fi + local log_size + log_size=$(stat -c '%s' "$LOG_FILE" 2>/dev/null || echo 0) + local log_size_h + log_size_h=$(du -h "$LOG_FILE" | awk '{print $1}') + if confirm "Rotate log file? (current size: ${log_size_h})"; then + mv "$LOG_FILE" "${LOG_FILE}.old" + ok "Rotated: ${LOG_FILE} → ${LOG_FILE}.old" + fi +} + OPTIONS=( Add "Download, review & install cron schedule" Remove "Remove cron schedule & local script" Update "Update local script from repository" + Status "Show installation status & last run" + Run "Run update script now (manual trigger)" View "View currently installed script" + Rotate "Rotate log file" ) -CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Cron Update LXCs" --menu "Select an option:" 12 68 4 \ +CHOICE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Cron Update LXCs" --menu "Select an option:" 16 68 7 \ "${OPTIONS[@]}" 3>&1 1>&2 2>&3) || exit 0 case $CHOICE in "Add") add ;; "Remove") remove ;; "Update") update_script ;; +"Status") show_status ;; +"Run") run_now ;; "View") view_script ;; +"Rotate") rotate_log ;; esac diff --git a/tools/pve/update-lxcs-cron.sh b/tools/pve/update-lxcs-cron.sh index 30b73527..35cddac4 100644 --- a/tools/pve/update-lxcs-cron.sh +++ b/tools/pve/update-lxcs-cron.sh @@ -1,9 +1,8 @@ #!/usr/bin/env bash # Copyright (c) 2021-2026 community-scripts ORG -# Author: tteck (tteckster) | Rewritten by community-scripts -# License: MIT -# https://github.com/community-scripts/ProxmoxVED/raw/main/LICENSE +# Author: MickLesk (CanbiZ) +# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE # # This script is installed locally by cron-update-lxcs.sh and executed # by cron. It updates all LXC containers using their native package manager. @@ -26,16 +25,26 @@ if [[ -f "$CONF_FILE" ]]; then fi function update_container() { - container=$1 - name=$(pct exec "$container" hostname) - echo -e "\n [Info] Updating $container : $name \n" + local container=$1 + local name + name=$(pct exec "$container" hostname 2>/dev/null || echo "unknown") + local os os=$(pct config "$container" | awk '/^ostype/ {print $2}') + echo -e "\n [Info] Updating $container : $name (os: $os)" case "$os" in alpine) pct exec "$container" -- ash -c "apk -U upgrade" ;; archlinux) pct exec "$container" -- bash -c "pacman -Syyu --noconfirm" ;; fedora | rocky | centos | alma) pct exec "$container" -- bash -c "dnf -y update && dnf -y upgrade" ;; - ubuntu | debian | devuan) pct exec "$container" -- bash -c "apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confold" dist-upgrade -y; rm -rf /usr/lib/python3.*/EXTERNALLY-MANAGED" ;; + ubuntu | debian | devuan) pct exec "$container" -- bash -c ' + apt-get update || { + echo "Acquire::By-Hash \"no\";" >/etc/apt/apt.conf.d/99no-by-hash + rm -rf /var/lib/apt/lists/* + apt-get update + } + DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confold" dist-upgrade -y + rm -rf /usr/lib/python3.*/EXTERNALLY-MANAGED' ;; opensuse) pct exec "$container" -- bash -c "zypper ref && zypper --non-interactive dup" ;; + *) echo " [Warn] Unknown OS type '$os' for container $container, skipping" ;; esac } @@ -52,16 +61,19 @@ for container in $(pct list | awk '{if(NR>1) print $1}'); do sleep 1 else status=$(pct status "$container") - template=$(pct config "$container" | grep -q "template:" && echo "true" || echo "false") - if [ "$template" == "false" ] && [ "$status" == "status: stopped" ]; then + if pct config "$container" 2>/dev/null | grep -q "^template:"; then + echo -e "[Info] Skipping template $container" + continue + fi + if [ "$status" == "status: stopped" ]; then echo -e "[Info] Starting $container" pct start "$container" sleep 5 - update_container "$container" + update_container "$container" || echo " [Error] Update failed for $container" echo -e "[Info] Shutting down $container" - pct shutdown "$container" & + pct shutdown "$container" --timeout 60 & elif [ "$status" == "status: running" ]; then - update_container "$container" + update_container "$container" || echo " [Error] Update failed for $container" fi fi done