Score:0

How to wait on /var/lib/apt/lists/lock availability on unattended execution?

cn flag

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

muru avatar
us flag
Did you try https://askubuntu.com/a/373478/158442? Or the `aptdcon` mentioned in the other answers there?
user535733 avatar
cn flag
Seems like a possible race condition. Is this an automated new-install? Or does the same issue occur on old installs when you run the script?
Alan Franzoni avatar
cn flag
It cannot be a race condition. All tasks are sequentials. If something "races" is something that packagekitd is doing on its own. The same issue happens every time I launch the script.
Alan Franzoni avatar
cn flag
@muru the fuser approach worked.
Score:0
cn flag
#!/bin/sh

while sudo fuser /var/lib/apt/lists/lock >/dev/null 2>&1; do
   sleep 1
done

This worked. From How to make a package manager wait if another instance of APT is running?

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.