Issue
I often fail to run apt-get install on my server. Like that:
$ sudo apt-get install tmux
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?
Happens a lot, almost every day. Whenever I try to find out who else is locking it with the lsof
command, then I am too late and the lock is already gone. Really very weird.
Is there a trick, how I can record for over 24 hrs who else is locking /var/lib/dpkg/lock
?
Solution
I think there are multiple solutions.
1: write a simple script, for example while sleep 1; do lsof -n|grep /var/lib/dpkg/lock >>/var/log/dpkglocktmp.log; done
. It is simple, but not very beautiful, but as a short-time bugtracking, it is to me okay. You will get the list of the processes in a log file.
Extension: while sleep 1; do lsof -n|grep /var/lib/dpkg/lock; done|tee --append /var/log/dpkglocktmp.log
will simultaneously write to the console and into the logfile. If you won't keep your ssh connection alive, you can run this in a screen.
2: Linux kernel has a functionality inotify
, which enables processes to get signaled on events happened on files they are watching. There are numerous tools around it, the most known is incron
. It enables you to call a script on the case of a file event. Probably there more complex tools as well, targeting exactly the problems similar to yours.
I had most similar problems as an apt update/install asked from me something, I forgot that and later tried to run another apt in another ssh session.
Answered By - peterh