b990fbb70c080db20d91855c4c1d46931cd54bc1
[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 /usr/sbin/checkrestart -v -i bash -b /a/bin/distro-setup/checkrestart-blacklist | \
17 sed '/^Found 0 processes using old versions of upgraded files$/d' >/var/log/checkrestart.log
18
19 if [[ -s /var/log/checkrestart.log ]]; then
20 for x in {30..1}; do
21 echo "pid $PID. unattended upgrade, rebooting in $((x*10)) seconds" | wall -n
22 sleep 10
23 done
24 for x in {30..1}; do
25 if ! fuser /var/lib/dpkg/lock &> /dev/null; then
26 /sbin/reboot
27 exit 0
28 fi
29 echo "pid $PID. unattended upgrade reboot waiting 10 seconds for dpkg lock" | wall -n
30 sleep 10
31 done
32 echo "pid $PID. dpkg locked for 5 minutes, automatic reboot failed" | pee cat "wall -n"
33 fi