45

I want to know the exact process that is using the lock file. I don't want to fix it, but just to know what is my system doing. How can I find out?

For example, this message:

E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

Again, I just want to identify the process using it, not how to fix it.

0

2 Answers 2

47

There is the infamous lsof:

sudo lsof /var/lib/dpkg/lock
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
aptitude 4974 root    3uW  REG   8,23        0 815673 /var/lib/dpkg/lock

In this case aptitude is using the file. You should use root in case you are not sure which user is locking the file. It's useful for a bunch of things too, sadly it doesn't come installed with Ubuntu, so you have to install it first.

For the rest of mortals, there's the fuser command. This is peculiar since it only returns the PID instead of the name of the process:

➜  ~  sudo fuser /var/lib/dpkg/lock
/var/lib/dpkg/lock:   4974

Here it says that the file and PID, which is 4974, so we must investigate who is:

➜  ~  ps 4974
  PID TTY      STAT   TIME COMMAND
 4974 pts/1    Sl+    0:06 aptitude
4
  • 6
    Why is lsof infamous? Are there some problems with it?
    – Ruslan
    Jan 19, 2014 at 9:29
  • 3
    @Ruslan Yeah, it allows clueless users know the actual usage of the systems contrary to what the sysadmins wants...
    – Braiam
    Jan 19, 2014 at 11:09
  • @Braiam Too much information can be a dangerous thing, in the right hands? ^-_-^ May 7, 2016 at 16:27
  • Second option in one line: ps $(sudo fuser /var/lib/dpkg/lock)
    – wjandrea
    Aug 1, 2017 at 17:27
11

lslocks is a simple way to do that.

lslocks |grep /var/lib/dpkg/lock

For use shell to handle the process id:

for pid in `lslocks -rn | grep /var/lib/dpkg/lock|awk '{print $2}'`;
do 
    echo $pid; 
done
2
  • Yes. lslocks is the best way to go about it. Using lsof gave me this error: "lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs Output information may be incomplete"
    – Nav
    Oct 15, 2017 at 6:26
  • @Nav that's not an error, but a WARNING.
    – Braiam
    Nov 14, 2018 at 15:56

You must log in to answer this question.

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