forked from HariSekhon/DevOps-Bash-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apt_wait.sh
executable file
·66 lines (56 loc) · 1.57 KB
/
apt_wait.sh
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
61
62
63
64
65
66
#!/usr/bin/env bash
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2020-06-18 20:03:00 +0100 (Thu, 18 Jun 2020)
#
# https://github.com/HariSekhon/DevOps-Bash-tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
# Blocking wait on apt-get locks to allow multiple apt-get calls to wait instead of exiting with errors
#
# Not really a new idea, there are similar implementations eg.
#
# https://gist.github.com/tedivm/e11ebfdc25dc1d7935a3d5640a1f1c90
set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
sleep_secs=1
locks="
/var/lib/dpkg/lock
/var/lib/apt/lists/lock
"
unattended_upgrade_log="/var/log/unattended-upgrades/unattended-upgrades.log"
sudo=""
[ $EUID = 0 ] || sudo=sudo
check_bin(){
if ! type -P "$1" &>/dev/null; then
echo "$1 not found in \$PATH ($PATH)" >&2
exit 1
fi
}
check_bin apt-get
check_bin fuser
if [ -n "$sudo" ]; then
check_bin "$sudo"
fi
while true; do
for lock in $locks; do
if $sudo fuser "$lock" &>/dev/null; then
echo "apt lock in use ($lock), waiting..." >&2
sleep "$sleep_secs"
continue 2
fi
done
if [ -f "$unattended_upgrade_log" ] &&
$sudo fuser "$unattended_upgrade_log" &>/dev/null; then
echo "apt unattended upgrade log in use ($unattended_upgrade_log), waiting..." >&2
sleep "$sleep_secs"
continue
fi
break
done