Files
Tools/apt-unlock/apt-unlock
T
LyAhn 8e37925205 add apt-unlock script
Used for when there is a phantom lock on the apt process preventing updating and upgrading
2026-06-08 21:18:39 +00:00

43 lines
980 B
Bash
Executable File

#!/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