shellcheck, better automated upgrades
[distro-setup] / filesystem / usr / local / bin / myupgrade
1 #!/bin/bash
2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4
5 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
6
7 set -eE -o pipefail
8 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
9
10 hn=$(hostname -f)
11 source /a/bin/bash_unpublished/source-state
12 if [[ $HOSTNAME == "$MAIL_HOST" || $hn == li.b8.nz ]]; then
13 exit 0
14 fi
15
16 if [[ $(/usr/sbin/checkrestart | wc -l) != 1 ]]; then
17 for x in {30..1}; do
18 echo "pid $PID. unattended upgrade, rebooting in $((x*10)) seconds" | wall -n
19 sleep 10
20 done
21 for x in {30..1}; do
22 if ! fuser /var/lib/dpkg/lock &> /dev/null; then
23 /sbin/reboot
24 exit 0
25 fi
26 echo "pid $PID. unattended upgrade reboot waiting 10 seconds for dpkg lock" | wall -n
27 sleep 10
28 done
29 echo "pid $PID. dpkg locked for 5 minutes, automatic reboot failed" | pee cat "wall -n"
30 fi