add apt-unlock script

Used for when there is a phantom lock on the apt process preventing updating and upgrading
This commit is contained in:
2026-06-08 21:18:39 +00:00
parent c282ff0bf9
commit 8e37925205
2 changed files with 45 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
# apt-unlock
This script is used for when a phantom lock is holding any apt session preventing updates/upgrades
+42
View File
@@ -0,0 +1,42 @@
#!/usr/bin/env bash
set -euo pipefail
LOCK_FILES=(
/var/lib/dpkg/lock
/var/lib/dpkg/lock-frontend
/var/lib/apt/lists/lock
/var/cache/apt/archives/lock
)
killed=0
for lock in "${LOCK_FILES[@]}"; do
if [ ! -f "$lock" ]; then
continue
fi
pid=$(sudo fuser "$lock" 2>/dev/null || true)
if [ -z "$pid" ]; then
continue
fi
pid=$(echo "$pid" | tr -s ' ' '\n' | grep -v '^$' | head -1)
name=$(ps -p "$pid" -o comm= 2>/dev/null || echo "unknown")
echo "Killing PID $pid ($name) holding $lock"
sudo kill -9 "$pid" 2>/dev/null || true
killed=$((killed + 1))
done
if [ $killed -eq 0 ]; then
echo "No apt locks held by any process."
else
# Wait briefly and clean up any stale lock files
sleep 1
for lock in "${LOCK_FILES[@]}"; do
if [ -f "$lock" ] && ! sudo fuser "$lock" &>/dev/null 2>&1; then
sudo rm -f "$lock"
fi
done
echo "Done. $killed lock(s) cleared."
fi