如何知道是否有可用的更新?

如何知道是否有可用的更新?

我正在运行 12.04 LTS ubuntu 服务器。我认为如果能在有更新可用时通知我,那就太好了。但我找不到如何知道...

我尝试查看apt-get手册页。从中我能够使用apt-get -s upgradeapt-get 输出脚本,而不会被问题所阻碍。

现在,我清楚地看到了区别:

有可用更新:

apt-get -s upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be upgraded:
  dpkg dpkg-dev libdpkg-perl
3 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Inst dpkg [1.16.1.2ubuntu7.2] (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [amd64])
Conf dpkg (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [amd64])
Inst dpkg-dev [1.16.1.2ubuntu7.2] (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [all]) []
Inst libdpkg-perl [1.16.1.2ubuntu7.2] (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [all])
Conf libdpkg-perl (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [all])
Conf dpkg-dev (1.16.1.2ubuntu7.3 Ubuntu:12.04/precise-updates [all])

无法更新:

apt-get -s upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

但我不知道如何继续。如何从 bash 脚本(或 php 脚本)判断是否有可用更新?

编辑 :

这是我当前的 bash 代码。它不起作用。

updates_available=`/etc/update-motd.d/90-updates-available`

if [ "${updates_available}" = "0 packages can be updated. 0 updates are security updates." ];
then
   echo "No updates are available"
else
   echo "There are updates available"
fi

答案1

阅读手册页 motd(5)pam_motd(8)update-motd(5)在我的系统上,当我登录时,/etc/update-motd.d/90-updates-available调用将显示以下内容:/usr/lib/update-notifier/update-motd-updates-available

19 packages can be updated.
12 updates are security updates.

深入研究一下,“...-updates-available”脚本会调用/usr/lib/update-notifier/apt-check --human-readable。如果你读过(python),你会发现如果你省略了人类可读的标志,它会将“19;12”输出到 stderr。我们可以用这个来获取它:

IFS=';' read updates security_updates < <(/usr/lib/update-notifier/apt-check 2>&1)
echo $updates
echo $security_updates 
19
12

现在你可以说:

if (( updates == 0 )); then
    echo "No updates are available"
else
    echo "There are updates available"
fi

相关内容