15

If you are running apt-get commands on terminal and want to install stuff on the software center, the center says it waits until apt-get finishes. I wanted to know if it is possible to do the same but on the terminal, i.e., make apt-get on the terminal wait until the lock is released.

I found this link, that uses aptdcon to install stuff. I would like to know if:

  • Is it really not possible to do with apt-get?
  • Is aptdcon compatible with apt-get, i.e., can I use both to install stuff without borking the system?
5
  • does apt-get not notify you if you operate in the software center?
    – blissini
    Commented Nov 12, 2015 at 11:17
  • What do you mean notify?
    – Camandros
    Commented Nov 12, 2015 at 11:27
  • if you run apt-get <some command> in the terminal, it will tell you that a different process is locking the package database (e.g. the software center is operating). So the system should already behave like you want it.
    – blissini
    Commented Nov 12, 2015 at 12:32
  • 1
    apt-get has no built-in functionality for this purpose.
    – muru
    Commented Nov 12, 2015 at 13:50
  • You should consider choosing this answer unix.stackexchange.com/a/277255/3285 as it's fixed now. Commented Apr 22, 2022 at 18:37

4 Answers 4

13

apt 1.9.11

This was solved in Debian bug #754103 in this commit. The fix is in versions of apt newer than 1.9.11.

  • apt(8): Wait for lock (Closes: #754103)

You can enable this option by setting -o DPkg::Lock::Timeout=60 as an argument to apt or apt-get. Where 60 is the time to wait in seconds for the lock.

apt -o DPkg::Lock::Timeout=60 install FOO
apt-get -o DPkg::Lock::Timeout=60 install FOO

You can test this by running two identical commands and simply not answering immediately on the first one to Do you want to continue? [Y/n]? On the second command you run, it'll tell you,

Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 946299 (apt)

4
  • 1
    It is fixed in apt version 1.9.11, newer of which is available e.g. in Ubuntu 20.04.
    – jarno
    Commented May 14, 2020 at 22:06
  • 1
    Note that only apt has this feature, not apt-get.
    – jarno
    Commented May 15, 2020 at 5:57
  • 1
    I've updated this answer to reflect the comment by @jarno and the ticket you linked. Commented Apr 22, 2022 at 18:36
  • 1
    As of apt 2.4.10, on Ubuntu 22.04, this DOES NOT WORK FOR "update". It works for "apt(-get) upgrade", but if you run two "update" at the same time one of them will still fail instead of waiting.
    – bgdnlp
    Commented Sep 25, 2023 at 10:57
10

(Repasting from Ask ubuntu)

There is now an option built into apt-get that lets you wait for the other apt to finish.

Use the DPkg::Lock::Timeout option to set a timeout, in seconds, for an apt-get command. This example will wait for 60 seconds:

sudo apt-get -o DPkg::Lock::Timeout=60 install packagename

If you set that value to -1, it will keep waiting forever.

sudo apt-get -o DPkg::Lock::Timeout=-1 install packagename

For more information see: Waiting for apt locks without the hacky bash scripts. This option was added to apt-get in Feb 2020.

1

you can do this already:

create a new script called apt-get (wrapper for apt-get) in the

/usr/local/sbin

directory with the following bash code inside:

#!/bin/bash
#Make sure there is no space before the hashbang above!
i=0 
tput sc 
while fuser /var/lib/dpkg/lock >/dev/null 2>&1 ; do
     case $(($i % 4)) in
         0 ) j="-" ;;
         1 ) j="\\" ;;
         2 ) j="|" ;;
         3 ) j="/" ;;
     esac
     tput rc
     echo -en "\r[$j] Waiting for other software managers to finish..." 
     sleep 0.5
     ((i=i+1)) 
done
/usr/bin/apt-get "$@"

Don't forget to make it executable:

sudo chmod +x /usr/local/sbin/apt-get

run it. run it again with synaptic or another apt-get open. ;)

/usr/local/sbin comes earlier in the path...

I think it could cause chaos with scripts somewhere... maybe?

2
  • 2
    Should probably acknowledge the author: askubuntu.com/questions/132059/…
    – adeelx
    Commented Oct 4, 2018 at 17:44
  • It did not work in my case.. I got: [/] Waiting for other software managers to finish...E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable) E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
    – vvvvv
    Commented Nov 18, 2019 at 13:40
0

For people who are using the older version (< 1.9.11) the above-mentioned methods might not work so here is my hacky version of the solution.

#!/bin/bash

timeout=12
wait_time=5
counter=0

while (( counter < timeout )); do
    if sudo apt install jq -y 2>/dev/null; then
        echo "jq installed successfully."
        exit 0
    else
        echo "Installation failed. Waiting for other software managers to finish..."
        sleep $wait_time
        ((counter++))
    fi
done

echo "Could not install jq within $((timeout * wait_time)) seconds. Skipping installation."

and here is a minified version too

for i in {1..12}; do sudo apt install jq -y && exit 0 || sleep 5; done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .