3577864d76363fbbb71ba0fd1f81850a286b217e
[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 # Note: running this inside a cronjob, it wont mail any output if we end
6 # up rebooting from this script.
7
8 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
9
10 source /usr/local/lib/err
11 pre="${0##*/}:"
12 PATH="/sbin:$PATH"
13 m() { printf "$pre %s\n" "$*"; "$@"; }
14 e() { printf "$pre %s\n" "$*"; }
15 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $pre: $*" >&2; }
16
17 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
18
19 hn=$(hostname -f)
20 source /a/bin/bash_unpublished/source-state
21
22 myreboot() {
23 for x in {30..1}; do
24 echo "pid $$. unattended upgrade, rebooting in $((x*10)) seconds" | wall -n
25 sleep 10
26 done
27 for x in {30..1}; do
28 if ! fuser /var/lib/dpkg/lock &> /dev/null; then
29 echo "pid $$. unattended upgrade, rebooting now" | pee cat "wall -n"
30 /a/bin/ds/keyscript-on
31 /sbin/reboot
32 exit 0
33 fi
34 echo "pid $$. unattended upgrade reboot waiting 10 seconds for dpkg lock" | wall -n
35 sleep 10
36 done
37 echo "pid $$. dpkg locked for 5 minutes, automatic reboot failed" | pee cat "wall -n"
38 }
39
40
41 # TODO: executed from cron, this doesnt sent an email when we reboot,
42 # because rebooting from the script stops the ability to send email.
43 # We should figure some workaround.
44
45 if ! out=$(/sbin/needrestart -p 2>&1); then
46 printf "%s\n\n" "$out"
47 if [[ $HOSTNAME != "$MAIL_HOST" ]]; then
48 myreboot
49 fi
50 fi