Please note: there're similar questions around, but this specific scenario is unanswered. Don't close this as a duplicate of 335794 or 1340943.
This is always reproducible, and this is about apt lists lock not about dpkg lock.
I'm running a series of automated tasks that perform some install through APT. The tasks are sequential (zero parallelism) but may invoke apt multiple times. At a certain point, when the script executes
sudo apt -o DPkg::Lock::Timeout=60 update
I immeditately get this error output:
Reading package lists... Done
E: Could not get lock /var/lib/apt/lists/lock. It is held by process 1496 (packagekitd)
N: Be aware that removing the lock file is not a solution and may break your system.
E: Unable to lock directory /var/lib/apt/lists/
this error goes away quite quickly - if I retry after 10 seconds the operation (usually) succeeds, but sometimes it stays locked longer because packagekitd is doing who-knows-what.
My problem: this is an unattended operation. I'd like to have this automated, but I couldn't find a way to either:
- tell apt to wait until the lists lock is available, or
- to manually wait for the lock - I tried using flock even with an exclusive lock, but acquiring it seems to always succeed (it's probably using another locking approach)
Surely I could sleep every time. But this is flaky and it will makes the process quite slow.
OS: Ubuntu 22.04 Intel 64bit