Running script
#!/bin/bash
(
flock 9
# ... commands executed under lock ...
fuser -v /var/lib/dpkg/lock
apt-get -f --assume-no install
) 9>/var/lib/dpkg/lock
as superuser does not display an error message. But if there is e.g. synaptic
running,apt-get
will display an error message:
"E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)".
dpkg
(and in turn apt) doesn't useflock(2)
for locking. Checking the system calls, involved, it seems they usefcntl(2)
:And from this SO post:
So
flock
isn't effective in locking it against other package management commands. (Thinking about it... if it were, then the subsequentapt-get
would have failed anyway.)The simplest way I can think of is to create an immutable
/var/lib/dpkg/lock
file for the duration of the task.Or you can write a short C program (or any language that provides an easy interface to
fcntl
) that usesfcntl
to lock it the way dpkg does.