#!/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.
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 use
flock(2)for locking. Checking the system calls, involved, it seems they use
And from this SO post:
flockisn't effective in locking it against other package management commands. (Thinking about it... if it were, then the subsequent
apt-getwould have failed anyway.)
The simplest way I can think of is to create an immutable
/var/lib/dpkg/lockfile 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 uses
fcntlto lock it the way dpkg does.