有没有一种干净的方法来检测 apt-get upgrade 或 apt-get dist-upgrade 上次在 Ubuntu 服务器上运行的时间?
如果没有默认方法执行此操作,那么修改脚本的最佳位置是哪里,以便跟踪 apt-get upgrade 或 apt-get dist-upgrade 上次运行的时间。
答案1
您正在搜索的日志文件是:/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
答案2
对于“我们上次修补这个问题是什么时候?”这个问题,我们能找到一个更简单、更准确的答案,这对我们来说很有用。所以我把这些放在一起。我在 12.04、14.04 和 16.04 上进行了测试。它为这个问题提供了相当准确的答案。
注意:“合理准确”可能并不意味着“完全准确”。
注意:仅针对该问题。
(建设性)欢迎评论!
#!/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";
}