Bash-如果apt-get/dpkg正在运行,如何检查软件包是否可以安装? [英] Bash - how to check if packages can be installed, if apt-get/dpkg is running?
问题描述
在bash脚本中,我想安装一个软件包.在理智地这样做之前,我需要检查apt-get或dpkg的其他实例是否已在工作.如果是这种情况,则apt-get将会失败,因为它已经被锁定.
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.
检查/var/lib/dpkg/lock和/var/lib/apt/lists/lock是否存在以及是否都不存在,安装是否安全?
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?
推荐答案
这取决于您处理apt-get错误的程度. 根据您的需要检查/var/lib/dpkg/lock和/var/lib/apt/lists/lock很好,但是如果您要格外小心,可以进行模拟并检查返回代码,如下所示:>
It depends how well you want to handle apt-get errors. For your needs checking /var/lib/dpkg/lock and /var/lib/apt/lists/lock is fine, but if you want to be extra cautious you could do a simulation and check the return code, like this:
if sudo apt-get --simulate install packageThatDoesntExist
then echo "we're good"
else echo "oops, something happened"
fi
例如:
which will give for instance:
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package packageThatDoesntExist
oops, something happened
--simulate将不会检查锁,因此您可能要在此处进行其他检查.如果要单独检查sudo,则可能还需要删除sudo
.
--simulate will not check for locks, so you might want to do an additional check there. You might also want to remove sudo
, if you want to check for sudo separately.
这篇关于Bash-如果apt-get/dpkg正在运行,如何检查软件包是否可以安装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!