-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoupdate.sh
More file actions
executable file
·60 lines (47 loc) · 1.42 KB
/
autoupdate.sh
File metadata and controls
executable file
·60 lines (47 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env bash
set -Eeuo pipefail
IFS=$'\n\t'
if [[ "${DEBUG:-0}" == "1" ]]; then
set -x
fi
PATH="${PATH:-/usr/bin:/bin:/usr/sbin:/sbin}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
export DEBIAN_FRONTEND=noninteractive
log_default() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] autoupdate: $*" >&2
}
NEEDRESTART_FAILED=0
run_autoupdate() {
local logger="${1:-log_default}"
NEEDRESTART_FAILED=0
if ! command -v "$logger" >/dev/null 2>&1; then
logger="log_default"
fi
if ! command -v apt-get >/dev/null 2>&1; then
"$logger" "apt-get not found; cannot continue"
return 1
fi
"$logger" "refreshing package lists"
apt-get update -y
"$logger" "calculating planned upgrades"
if simulation_output="$(apt-get dist-upgrade -s)"; then
PENDING_UPGRADES="$(printf '%s\n' "$simulation_output" | grep -c '^Inst ' || true)"
"$logger" "planned upgrades: ${PENDING_UPGRADES}"
else
"$logger" "failed to simulate dist-upgrade"
return 1
fi
"$logger" "applying dist-upgrade"
apt-get dist-upgrade -y
if command -v needrestart >/dev/null 2>&1; then
"$logger" "restarting affected services via needrestart"
if ! needrestart -r a; then
"$logger" "needrestart reported failures; scheduling reboot"
NEEDRESTART_FAILED=1
fi
fi
"$logger" "running autoremove with purge"
apt-get autoremove --purge -y
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
run_autoupdate
fi