Offer to reboot at the end of an install or update that needs one
shellcheck / shellcheck (push) Failing after 13s

update already said "Please reboot when convenient" after a new kernel,
firmware, microcode, NVIDIA driver, systemd or glibc. It now ends by
asking "Reboot now? [y/N]", after the offer to send a failed step's
log. 99-reboot-check.sh records that a reboot is needed, and update
clears that before its steps run, so an earlier update's reboot isn't
offered. Under script(1), the copy outside reboots once the log is
cleaned up. Never asked without a terminal, or when the initramfs
failed to rebuild.

install asks the same when it brought in a newer kernel.

Both reboot with systemctl, like the system menu's restart: sudo's
cached password belongs to another terminal by then.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 06:17:53 -07:00
co-authored by Claude Opus 5.5
parent 902967f111
commit 5d7db2cdc8
6 changed files with 157 additions and 4 deletions
+33 -1
View File
@@ -44,6 +44,31 @@ clean_log() {
s/\x1b[()][0-9A-Za-z]//g; s/\x1b[=>]//g; s/\r+$//; s/.*\r//' "$1"
}
# Set by updates.d/99-reboot-check.sh when this update needs a reboot
REBOOT_NEEDED="$HOME/.local/state/coredesktop/reboot-needed"
# The same as the system menu's restart: an active desktop session needs no
# password. (sudo's cached password belongs to script(1)'s terminal, see below.)
reboot_now() {
echo "Rebooting..."
systemctl reboot || echo "Couldn't reboot. Restart from the system menu instead."
}
# If this update needs a reboot, ask on the terminal whether to reboot now. Under
# script(1), leave it to the copy outside, so the log is cleaned up first.
offer_reboot() {
local tty="${COREDESKTOP_TTY:-/dev/tty}" answer
[ -e "$REBOOT_NEEDED" ] || return 0
{ : < "$tty"; } 2> /dev/null || return 0
read -rp "Reboot now? [y/N] " answer < "$tty" || return 0
[[ "$answer" == [yY]* ]] || return 0
if [ -n "$UPDATE_IN_SCRIPT" ]; then
touch "$UPDATE_LOG.reboot"
else
reboot_now
fi
}
# Log each update to ~/.local/state/coredesktop/logs (95-cleanup.sh deletes
# old ones). In a terminal, run under script(1), so everything still sees a
# terminal and colors, menus and prompts work as before; otherwise use tee.
@@ -52,9 +77,13 @@ if [[ -z "$UPDATE_LOG" ]] && mkdir -p -m 700 "$HOME/.local/state/coredesktop/log
UPDATE_LOG="$HOME/.local/state/coredesktop/logs/update-$(date +%Y-%m-%d-%H%M%S).log"
export UPDATE_LOG
if [ -t 0 ] && [ -t 1 ] && command -v script > /dev/null; then
SHELL=/bin/bash script -q -e -f -c "$(printf '%q ' "$0" "$@")" "$UPDATE_LOG"
SHELL=/bin/bash UPDATE_IN_SCRIPT=1 script -q -e -f -c "$(printf '%q ' "$0" "$@")" "$UPDATE_LOG"
status=$?
clean_log "$UPDATE_LOG"
if [ -e "$UPDATE_LOG.reboot" ]; then
rm -f "$UPDATE_LOG.reboot"
reboot_now
fi
exit "$status"
fi
exec > >(tee -a "$UPDATE_LOG") 2>&1
@@ -118,6 +147,7 @@ while kill -0 $$ 2>/dev/null; do sudo -n -v; sleep 60; done > /dev/null 2>&1 &
SUDO_KEEPALIVE_PID=$!
trap 'kill "$SUDO_KEEPALIVE_PID" 2>/dev/null' EXIT
rm -f "$REBOOT_NEEDED"
FAILED=()
[[ -n "$UPDATE_PULL_FAILED" ]] && FAILED+=("git pull")
@@ -150,9 +180,11 @@ if (( ${#FAILED[@]} > 0 )); then
# Ask, on the terminal if there is one, whether to send it to us
[ -x ~/.local/bin/report ] && ~/.local/bin/report --offer "$UPDATE_LOG" "${FAILED[@]}"
fi
offer_reboot
exit 1
fi
echo "Done."
offer_reboot
exit 0
}