#!/bin/bash # Copyright (C) 2019 Ian Kelling # SPDX-License-Identifier: AGPL-3.0-or-later if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR hn=$(hostname -f) source /a/bin/bash_unpublished/source-state if [[ $HOSTNAME == "$MAIL_HOST" || $hn == li.b8.nz ]]; then exit 0 fi cmd="$s /usr/sbin/checkrestart -p" if [[ $($cmd | sed '/^Found 0 processes using old versions of upgraded files$/d' | wc -l) != 0 ]]; then $cmd -v | pee cat "wall -n" fi if [[ -s /var/log/checkrestart.log ]]; then for x in {30..1}; do echo "pid $PID. unattended upgrade, rebooting in $((x*10)) seconds" | wall -n sleep 10 done for x in {30..1}; do if ! fuser /var/lib/dpkg/lock &> /dev/null; then /sbin/reboot exit 0 fi echo "pid $PID. unattended upgrade reboot waiting 10 seconds for dpkg lock" | wall -n sleep 10 done echo "pid $PID. dpkg locked for 5 minutes, automatic reboot failed" | pee cat "wall -n" fi