In a bash script I want to install a package. Before sanely doing so, I need to check if no other instance of apt-get
or dpkg
is already working. If that was the case, the apt-get
would fail, because its already locked.
Is it sufficient to check if /var/lib/dpkg/lock
and /var/lib/apt/lists/lock
exists and if both don't exist, installing is safe?
In Debian Wheezy (currently stable), those files always exist. Therefore I found using lsof /var/lib/dpkg/lock
to be a more useful check. It returns 1 if nothing is using the lock, 0 if it is, so:
lsof /var/lib/dpkg/lock >/dev/null 2>&1
[ $? = 0 ] && echo "dpkg lock in use"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With