如何知道上次`apt-get update`被执行了?

我知道更新存储库列表的命令是apt-get update

如何检查今天或过去24小时内是否已执行?

我不知道是否应该检查一些文件时间戳。 或者发出另一个apt命令。 或者使用dpkg实用程序。

在手册页找不到有用的东西。

您可以在终端中查看命令历史记录:

 history | grep 'apt update' 

要按时间检查:

 HISTTIMEFORMAT="%d/%m/%y %T " history | grep 'apt update' 

在此处输入图像描述

希望能帮助到你 !

检查/var/lib/apt/periodic/update-success-stamp

 $ ls -l /var/lib/apt/periodic/update-success-stamp -rw-r--r-- 1 root root 0 Jan 25 01:41 /var/lib/apt/periodic/update-success-stamp 

这里的时间是Jan 25 01:41当时apt-get最后执行。 要获得时间,请在终端中使用以下命令,

 $ ls -l /var/lib/apt/periodic/update-success-stamp | awk '{print $6" "$7" "$8}' Jan 25 01:41 

这是检查上次更新时间的最佳位置。 如果您发现/var/lib/apt/periodic/为空,您可以尝试,

 ls -l /var/log/apt/history.log 

更新

发现由于某些原因,上述文件update-success-stamphistory.log在某些系统中仍然不可用。 /var/cache/apt/pkgcache.bin 提出了一个新的建议来查看文件/var/cache/apt/pkgcache.bin

pkgcache.bin是Apt的内存映射包缓存位置。 每次更新后都会续订。 因此,知道apt更新的最后一次是完美的候选人。

可以使用以下命令知道确切的时间,

 ls -l /var/cache/apt/pkgcache.bin | cut -d' ' -f6,7,8 

要么

 stat /var/cache/apt/pkgcache.bin 

我使用/var/cache/apt来确定是否需要运行apt-get update 。 默认情况下,如果/var/cache/apt的当前时间和缓存时间之间的差异小于24小时,我不需要运行apt-get update 。 可以通过将数字传递给函数runAptGetUpdate()来覆盖默认更新间隔

 function trimString() { local -r string="${1}" sed -e 's/^ *//g' -e 's/ *$//g' <<< "${string}" } function isEmptyString() { local -r string="${1}" if [[ "$(trimString "${string}")" = '' ]] then echo 'true' else echo 'false' fi } function info() { local -r message="${1}" echo -e "\033[1;36m${message}\033[0m" 2>&1 } function getLastAptGetUpdate() { local aptDate="$(stat -c %Y '/var/cache/apt')" local nowDate="$(date +'%s')" echo $((nowDate - aptDate)) } function runAptGetUpdate() { local updateInterval="${1}" local lastAptGetUpdate="$(getLastAptGetUpdate)" if [[ "$(isEmptyString "${updateInterval}")" = 'true' ]] then # Default To 24 hours updateInterval="$((24 * 60 * 60))" fi if [[ "${lastAptGetUpdate}" -gt "${updateInterval}" ]] then info "apt-get update" apt-get update -m else local lastUpdate="$(date -u -d @"${lastAptGetUpdate}" +'%-Hh %-Mm %-Ss')" info "\nSkip apt-get update because its last run was '${lastUpdate}' ago" fi } 

样本输出:

 <~/ubuntu-cookbooks/libraries> # runAptGetUpdate Skip apt-get update because its last run was '0h 37m 43s' ago 

我从我的个人github中提取了这些函数: https : //github.com/gdbtek/ubuntu-cookbooks/blob/master/libraries/util.bash

您可能也对该文件感兴趣:

 /var/log/apt/term.log 

lesscat作为root打开它。

我用这个命令

stat /var/cache/apt/ | grep -i -e access -e modify

显示最后一次访问即。 上次运行’apt-get update’时实际更新了。

请注意,如果时间不同,可能没有可用的更新。 由于我在特定时间通过crontab运行更新和升级,因此可以判断我的更新是否已运行。

将@ ssokolow的最后评论与此处的答案相结合,如果该命令在过去7天内未运行,则该命令将运行apt-get update

 [ -z "$(find -H /var/lib/apt/lists -maxdepth 0 -mtime -7)" ] && sudo apt-get update 

说明:

  • -mtime -7查找过去7天内有更改时间的文件。 如果您关心的时间较短,可以使用-mmin
  • -maxdepth 0确保find不会进入目录的内容。
  • -H dereferences /var/lib/apt/lists如果是软链接
  • 如果由于某种原因find失败,则命令将运行。 在我看来,这似乎是安全的默认值。 如果要翻转默认值,请在test中使用-n ,在find命令中使用-mtime +7

我刚刚在以下主题上发布了这个问题的答案

我在哪里可以查看我的更新历史记录?

答案可能不太适合这个主题,因为它专门寻找“apt-get upgrade”。 这是示例输出。

 xenial% 9: ./linuxpatchdate 2016-07-19 54 2017-02-24 363 2017-03-08 7 2017-03-09 2 

有关源代码和更多说明,请参阅其他主题。