如何判断apt-get升级或apt-get dist-upgrade何时上次运行

有没有一种干净的方式来检测apt-get升级或apt-get dist-upgrade是否上次在Ubuntu服务器上运行?

如果默认情况下没有办法做到这一点,那么修改脚本的最佳位置是在哪里,以便跟踪apt-get升级或apt-get dist-upgrade最后一次运行的时间。

您正在search的日志文件是:/var/log/apt/history.log

这里有2个日志条目:

Start-Date: 2014-04-15 14:15:35 Commandline: apt-get install python3-tk Install: libxcb-dri2-0:amd64 (1.9.1-3ubuntu1, automatic), x11-utils:amd64 (7.7+1, automatic), tk8.5-lib:amd64 (8.5.11-2ubuntu4, automatic), tcl8.5-lib:amd64 (8.5.13-1ubuntu4, automatic), libllvm3.3:amd64 (3.3-5ubuntu4, automatic), libgl1-mesa-dri:amd64 (9.2.1-1ubuntu3, automatic), libglapi-mesa:amd64 (9.2.1-1ubuntu3, automatic), libtxc-dxtn-s2tc0:amd64 (0~git20121227-1, automatic), libxv1:amd64 (1.0.9-1, automatic), libutempter0:amd64 (1.1.5-4build1, automatic), libxss1:amd64 (1.2.2-1, automatic), libxcb-glx0:amd64 (1.9.1-3ubuntu1, automatic), libgl1-mesa-glx:amd64 (9.2.1-1ubuntu3, automatic), libdrm-nouveau2:amd64 (2.4.46-1ubuntu1, automatic), libxcb-shape0:amd64 (1.9.1-3ubuntu1, automatic), blt:amd64 (2.4z-7, automatic), tk8.5:amd64 (8.5.11-2ubuntu4, automatic), tcl8.5:amd64 (8.5.13-1ubuntu4, automatic), xterm:amd64 (278-1ubuntu3, automatic), libxxf86vm1:amd64 (1.1.3-1, automatic), libelf1:amd64 (0.157-1ubuntu1, automatic), libxxf86dga1:amd64 (1.1.4-1, automatic), python3-tk:amd64 (3.3.1-0ubuntu2) End-Date: 2014-04-15 14:15:44 Start-Date: 2014-04-23 11:10:37 Commandline: apt-get dist-upgrade Upgrade: mysql-server-core-5.5:amd64 (5.5.35-0ubuntu0.13.10.2, 5.5.37-0ubuntu0.13.10.1), mysql-server-5.5:amd64 (5.5.35-0ubuntu0.13.10.2, 5.5.37-0ubuntu0.13.10.1), mysql-client-core-5.5:amd64 (5.5.35-0ubuntu0.13.10.2, 5.5.37-0ubuntu0.13.10.1), mysql-client-5.5:amd64 (5.5.35-0ubuntu0.13.10.2, 5.5.37-0ubuntu0.13.10.1), mysql-common:amd64 (5.5.35-0ubuntu0.13.10.2, 5.5.37-0ubuntu0.13.10.1), libmysqlclient18:amd64 (5.5.35-0ubuntu0.13.10.2, 5.5.37-0ubuntu0.13.10.1), python3-distupgrade:amd64 (0.205.5, 0.205.6), mysql-server:amd64 (5.5.35-0ubuntu0.13.10.2, 5.5.37-0ubuntu0.13.10.1), ubuntu-release-upgrader-core:amd64 (0.205.5, 0.205.6) End-Date: 2014-04-23 11:11:10 

对于“我们最后一次修补这个东西的时间是什么?”这个问题,我们有一个更简单准确的答案对我们有用。 所以我把它放在一起。 我在12.04和14.04和16.04testing了它。 它为这个问题返回合理准确的答案。

注意:“合理准确”可能不是“完全准确”。

注意:仅限于“那个问题”。

(build设性)评论赞赏!

 #!/usr/bin/perl #------------------ subroutines -------------------- sub parseRecord { my $sdate = ""; my $useful = 0; my $packages = 0; my @ptmp; while (my $recordLine = shift() ) { if ($recordLine =~ m/^Start-Date: ([\d\-]*).*/) { $sdate = $1; } elsif ($recordLine =~ m/^Commandline:.*upgrade/) { $useful = 1; } elsif ($recordLine =~ m/^Install: (.*)/) { $recordLine =~ s/\([^\)]*\)//g; @ptmp = split(/,/,$recordLine); $packages = $packages + $#ptmp + 1; } elsif ($recordLine =~ m/^Upgrade: (.*)/) { $recordLine =~ s/\([^\)]*\)//g; @ptmp = split(/,/,$recordLine); $packages = $packages + $#ptmp + 1; } } if ($useful) { return ($sdate,$packages); } else { return ("0",0); } } #------------------ main program -------------------- @lines = split(/\n/,`/bin/zcat -f /var/log/apt/history.log /var/log/apt/history*gz`); my %patchHash; my $line; my @inputLines; my $pushDate = ""; my $pushNum = ""; foreach $line (@lines) { # all records separated by blank lines if ($line !~ /./) { # no-op } elsif ($line =~ m/^Start-Date: ([\d\-]*).*/) { @inputLines = (); push (@inputLines, $line); } elsif ($line =~ m/^End-Date: ([\d\-]*).*/) { ($pushDate, $pushNum) = parseRecord(@inputLines); if ($pushNum != 0) { $patchHash{$pushDate} += $pushNum; } } else { push (@inputLines, $line); } } foreach $pushDate (sort(keys(%patchHash))) { print "$pushDate $patchHash{$pushDate}\n"; }