I need to check for dpkg lock before installing apps in a script

5,827

To identify processes using files or sockets you can use fuser command (see man fuser for more info). In case of dpkg, you can check for the lock as root with the following command:

fuser /var/lib/dpkg/lock
Share:
5,827

Related videos on Youtube

Noki
Author by

Noki

Primarily a software tester by trade. Though some simple dev work like automation. Love linux over the alternatives, much prefer to have a modular system that I can customise. I consider myself fairly intermediate level when it comes to linux knowledge. I know how to fix basic problems without google these days and seldom need help. I love askUbuntu, the answers I get are so clear and concise, often I even find the answer here before I ask it. Also a part-time forex trader.

Updated on September 18, 2022

Comments

  • Noki
    Noki over 1 year

    I have a problem with a script I wrote to install a bunch of apps, basically as it loops through an array of app names to install it checks them by doing:

    dpkg -l | grep -q $i
    

    and based on the $? will either install or skip the app. Trouble is for a few items it fails due to a dpkg lock, almost the whole list works, just half a dozen fail but work if you run the script again.

    Is it possible to wait for dpkg to finish before running the 'apt-get install'?

    For now I've added a 'sleep 0.5' between the 2 lines and this has stopped the problem but is obviously ugly and slower.

    Thanks, Noki

    • Admin
      Admin about 10 years
      How do I use this in a non-interactive script. This appears to need user input each time it queues up an application to install?
    • Admin
      Admin about 10 years
      You should better ask on the concerned answer.
  • jobin
    jobin about 10 years
    Your answer on this question is much better/helpful IMO.
  • Radu Rădeanu
    Radu Rădeanu about 10 years
    @Jobin This answer only to the current question.