2

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?

Michael Heuberger
  • 822
  • 3
  • 10
  • 27
  • maybe it's `/etc/cron.daily/apt`? – mata Apr 29 '15 at 23:33
  • For clarification, it happens anytime during the day and lasts a couple of seconds. Which means it cannot be from `/etc/cron.daily/apt` nor a cron job can solve it (because it only lasts few seconds) – Michael Heuberger Apr 30 '15 at 00:44

2 Answers2

2

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.

peterh
  • 11,875
  • 18
  • 85
  • 108
  • Cool. What modifications are needed if I wanted this while loop to display the output of lsof to the console while writing to the log file at the same time? And have it run forever without the need for keeping an SSH session open? This all day ... – Michael Heuberger Apr 30 '15 at 01:58
  • 1
    @MichaelHeuberger I extended it into my answer. In your case I used exactly this, from a screen. And screen is a wonderful tool, you can have 9 virtual character consoles in the background, detach them after work and reattach next day. – peterh Apr 30 '15 at 02:17
1

You could make a cron job that ran:

 lsof | grep /var/lib/dpkg/lock >> whodunit.txt

And try to catch them in the act.

Noelkd
  • 7,686
  • 2
  • 29
  • 43