Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have seen many softwares such as Update Manager and Synaptic Package Manager, they wait if some other program is using the /var/lib/dpkg/lock and is locked. How can we do this through the Terminal? I saw apt-get's manual but didn't find anything useful.

share|improve this question
well you can do several apt-get commands. Like sudo apt-get install packagename && sudo apt-get update and they will happen automatic after each others. – Alvar Nov 9 '13 at 11:10
Does this not do that: sudo apt-get install packagename1 packagename2 packagename3 ? – Rinzwind Nov 9 '13 at 11:45
up vote 24 down vote accepted

You can use the aptdcon command Manpage icon to queue up package manager tasks by communicating with aptdaemon instead of using apt-get directly.

So basically you can just do sudo aptdcon --install chromium-browser or whatever and while that command is running you can run it again but install different packages and apt-daemon will just queue them up instead of erroring out.

This is especially useful if you're doing a long upgrade or something and want to keep installing packages or if you're scripting something together and want to make sure installing things will be more reliable.

share|improve this answer
Yeah! This works Thankyou! :) – Piyush Jan 28 '14 at 11:41
Can aptdcon be run such that it accepts any prompts as apt-get -y does? – Noki Aug 16 '15 at 23:04
yes | aptdcon --hide-terminal --install "package" Hide Terminal is needed, else piping yes will cause issues. – whitehat101 Mar 17 at 17:58

You can make apt-get to learn to wait if another software manager is running. Something similar with the behaviour from the next screen cast:

enter image description here

How I made it?

I create a new script called apt-get (wrapper for apt-get) in /usr/local/sbin directory with the following bash code inside:


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="/" ;;
    tput rc
    echo -en "\r[$j] Waiting for other software managers to finish..." 
    sleep 0.5

/usr/bin/apt-get "$@"

Don't forget to make it executable:

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

Before to test, check if everything is ok. The output of which apt-get command should be now /usr/local/sbin/apt-get. The reason is: by default, the /usr/local/sbin directory is placed before /usr/bin directory in user or root PATH.

share|improve this answer
thats really a direct hit to my question. thanks :) – Raja Nov 10 '13 at 16:32
How your script manages stacked instances of apt-get? Like, before it finishes I launch 5 more apt-get? – Braiam Nov 11 '13 at 1:14
@Braiam Sincerely, I don't know; I'm not a good tester. Did you test it? If problems appears, the script can be improved by creating a new lock with timestamp when a new instance of the script starts (just an example - there are also many other ways). – Radu Rădeanu Nov 14 '13 at 21:03

Apart of the obvious &&, you may be looking for aptdcon. This tool is able to detect other instances of apt and wait them to finish:

sudo aptdcon --safe-upgrade
[/]  11% Waiting for other software managers to quit Waiting for aptitude

(I'm running aptitude somewhere else)

The advantage of this tool is that you can stock several actions consecutively without being worried of what you will be doing next. aptdcon is ideal for unattended scripts, and GUI installation, since you can allow the tool run in background as not to block your frontend.

The operations supported by aptdcon are:

  • --refresh, -c: This is the equivalent to apt-get update. It updates your package list.
  • --install, --remove, --upgrade, --purge, --downgrade. Each of them do as their names say. The name of the package(s) is mandatory. -i, -r, -u, -p: these are the short options for all except downgrade, who doesn't have one.
  • --safe-upgrade, --full-upgrade are the counterparts to apt-get's upgrade/dist-upgrade and aptitude's safe-upgrade/full-upgrade. These doesn't need parameters.
  • There are several others operations, which can be found in the manual. But, these are the most used by users interested in aptd. There are options that overlap with what apt-key, apt-cache, dpkg do.

apt-get itself doesn't support such methods (to wait for other instances of apt), so aptdcon is the preferred solution to GUI's package managers: USC uses aptd as back-end, same as Synaptic. Other solution is packagekit, but it doesn't support the function that you are looking for (yet).

share|improve this answer
aptdcon --install /path/to/pgk.deb Works like dpkg -i, although I wasn't able to find this explicitly mentioned in the manual. – whitehat101 Mar 17 at 18:05

A very simple approach would be a script that waited for the lock to not be open. Let's call it waitforapt and stick it in /usr/local/bin:


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

Then just run sudo waitforapt && sudo apt-get install whatever. You could add exceptions into sudoers to allow you to run it without needing a password (you'll need it for the apt-get so it's no great gain).

Unfortunately this doesn't queue things. Given that some of apt's operations are interactive ("Are you sure you want to remove all those packages?!"), I can't see a good way around this...

share|improve this answer
maybe -y to assume yes for all operations? – Braiam Nov 9 '13 at 13:36
Thank you. I think I can use it better if I use alias help. – Raja Nov 9 '13 at 14:29
In not sure -y is desirable though. – Oli Nov 9 '13 at 19:02
I don't think you want to wrap sudo fuser in [[ $(...) ]]. It returns a zero exit code when the file is being accessed so that should be sufficient. – minerz029 Nov 10 '13 at 9:15

You could using a polling technique:

$ time (while ps -opid= -C apt-get > /dev/null; do sleep 1; done); \
  apt-get -y install some-other-package
share|improve this answer

I made a script which does this:


# File path to watch

# tput escape codes
cr="$(tput cr)"
clr_end="$(tput el)"
up_line="$(tput cuu 1)"

    # Cleans the last two lines of terminal output,
    # returns the cursor to the start of the first line
    # and exits with the specified value if not False

    echo -n "$cr$clr_end"
    echo -n "$cr$clr_end$up_line"
    if [[ ! "$1" == "False" ]]; then
        exit $1

    # Takes the LOCKED variable, expected to be output from `lsof`,
    # then gets the PID and command line from `/proc/$pid/cmdline`.
    # It sets `$open_program` to a user friendly string of the above.

    pid=`echo $pid | sed 's/[\n\r ].*//'`
    while IFS= read -d '' -r arg; do
    done < "/proc/${pid}/cmdline"
    open_program="$pid : ${cmdline[@]}"

# Default starting value

# Checks if the file is locked, writing output to $FUSER
while LOCKED="$(lsof -F p "$LOCK_FILE" 2>/dev/null)" ; do
    # This will be true if it isn't the first run
    if [[ "$i" != 0 ]]; then
        case $(($i % 4)) in
            0 ) s='-'
                _get_cmdline # Re-checks the command line each 4th iteration
            1 ) s=\\ ;;
            2 ) s='|' ;;
            3 ) s='/' ;;
        # Traps to clean up the printed text and cursor position
        trap "CLEAN False; trap - SIGINT ; kill -SIGINT $$" SIGINT
        trap 'CLEAN $((128+15))' SIGTERM
        trap 'CLEAN $((128+1))' SIGHUP
        trap 'CLEAN $((128+3))' SIGQUIT

        # Default starting character

        echo -n "$save_cur"
    # Prints the 2nd line first so the cursor is at the end of the 1st line (looks nicer)
    echo -n "$cr$clr_end$open_program"
    echo -n "$up_line$res_cur$cr$clr_end[$s] Waiting for other package managers to finish..."
    #echo -en "$cr$clr_end[$s] Waiting for other package managers to finish..."
    #echo -en "\n$cr$clr_end$open_program$cr$up_line"
    sleep 0.025


# This allows saving the script under a different name (e.g. `apt-wait`)
# and running it. It only imitates `apt-get` if it was launched as such
if [[ "${0##*/}" == 'apt-get' ]]; then
    exec /usr/bin/apt-get "$@"
    exit $?

Save the above into /usr/local/sbin/apt-get. apt-get will then wait if another instance is already running.

Alternatively, save it as /usr/local/sbin/apt-wait, example usage:

apt-wait && aptitude

which will run aptitude after the current process holding the lock has exited.

Example run:

  1. First, an apt-get command is run, (don't run this command):

    $ sudo apt-get remove apt
  2. Then, in another terminal, another command is run:

    $ sudo apt-get install apt

    It will wait for the first command to finish then run. Output while waiting:

    [/] Waiting for other package managers to finish...
          28223 : /usr/bin/apt-get remove apt
share|improve this answer

Only one package manager can access the package directory while using apt-get. Either you have to quit an existing one to start a new one or you have to wait for one to finish to start a new one.

The commands corresponding to synaptic search and install are respectively "apt-cache search package" and "sudo apt-get install package".

share|improve this answer
But how can we make it wait if other apt-get is running? – Piyush May 5 '12 at 13:25
You'll have to wait manually or write a script that will start running the new apt-get once the old one is finished. But I don't understand a situation in which you would want to do that. Is there any problem manually waiting? Perhaps you could explain your situation so that we can help more. – harisibrahimkv May 5 '12 at 13:28
It will help me in the situation when like I have lots of packages to download and suddenly I got an important call and I have to go somewhere. I want to complete the download by the time I return. But right now some other program is using the apt-get so I cannot start my download. – Piyush May 6 '12 at 9:50

As far as I know, you can select multiple packages from synaptic and also you can provide multiple packages to apt-get. So all you need to do, is to provide apt-get with all the packages you want to install, that should do the trick.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.