14

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?
Camandros
  • 443
  • 3
  • 5
  • 13

3 Answers3

10

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)

Evan Carroll
  • 28,578
  • 45
  • 164
  • 290
imz -- Ivan Zakharyaschev
  • 15,113
  • 15
  • 61
  • 123
8

(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.

Mendhak
  • 623
  • 7
  • 10
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?

Potaito
  • 103
  • 3
Eric Sebasta
  • 131
  • 4
  • 2
    Should probably acknowledge the author: https://askubuntu.com/questions/132059/how-to-make-a-package-manager-wait-if-another-instance-of-apt-is-running – adeelx Oct 04 '18 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 Nov 18 '19 at 13:40