Skip to content

Instantly share code, notes, and snippets.

@lucasrangit
Last active February 17, 2022 13:09
Show Gist options
  • Save lucasrangit/53727223d33fa8d876936e467f9de3da to your computer and use it in GitHub Desktop.
Save lucasrangit/53727223d33fa8d876936e467f9de3da to your computer and use it in GitHub Desktop.
Monitor DPKG lock file to determine which command acquired it and list its parent tree to determine why/how it was executed.
#!/usr/bin/env bash
set -o xtrace
set -o errexit
set -o pipefail
while true; do
pid=$(set +o pipefail; sudo fuser /var/lib/dpkg/lock-frontend 2>&1 | grep -o "[0-9]*" | head -n 1)
if [[ ! -z $pid ]]; then
sudo pstree -sa $pid
fi
sleep 1
done
@lucasrangit
Copy link
Author

lucasrangit commented Feb 17, 2022

Output looks like this. Showing that it is the daily unattended upgrade cron job that is locking dpkg.

+++ set +o pipefail
+++ sudo fuser /var/lib/dpkg/lock-frontend
+++ grep -o '[0-9]*'
+++ head -1
++ pid=4176
++ [[ ! -z 4176 ]]
++ sudo pstree -sa 4176
systemd
  `-apt.systemd.dai /usr/lib/apt/apt.systemd.daily install
      `-apt.systemd.dai /usr/lib/apt/apt.systemd.daily lock_is_held install
          `-unattended-upgr /usr/bin/unattended-upgrade
              |-unattended-upgr /usr/bin/unattended-upgrade
              |   `-dpkg --status-fd 12 --configure --pending
              |       `-linux-image-5.4 /var/lib/dpkg/info/linux-image-5.4.0-1064-gcp.postinst triggered ...
              |           `-sh /usr/lib/linux/triggers/5.4.0-1064-gcp
              |               `-run-parts --report --exit-on-error --arg=5.4.0-1064-gcp --arg=/boot/vmlinuz-5.4.0-1064-gcp ...
              |                   `-grub-mkconfig /usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg
              `-2*[{unattended-upgr}]
++ sleep 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment