Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash - how to check if packages can be installed, if apt-get/dpkg is running?

Tags:

apt-get

dpkg

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?

like image 345
user28464 Avatar asked Feb 16 '23 16:02

user28464


1 Answers

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"
like image 106
Ryley Avatar answered Apr 25 '23 10:04

Ryley