这个服务器运行Debian 7,我正面临一个很大的谜团。
这是我的cron任务:
$ sudo crontab -e 42 15 * * * apt-get -y update >> /var/log/my-apt-update.txt 52 15 * * * apt-get -y upgrade >> /var/log/my-apt-upgrade.txt
我添加了">> /var/log/my-apt-upgrade.txt"部分,因为我试图理解为什么我的系统永远不会升级。
cron任务运行。 每天我在/ var / log / syslog中都有这些行:
Nov 14 15:42:01 myhostname /USR/SBIN/CRON[3374]: (root) CMD (apt-get -y update >> /var/log/my-apt-update.txt) Nov 14 15:52:01 myhostname /USR/SBIN/CRON[3394]: (root) CMD (apt-get -y upgrade >> /var/log/my-apt-upgrade.txt)
和/var/log/my-apt-upgrade.txt有这样的段落(我只显示最后两天):
Reading package lists... Building dependency tree... Reading state information... The following packages will be upgraded: file libmagic1 2 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. Need to get 0 B/256 kB of archives. After this operation, 110 kB disk space will be freed. Reading package lists... Building dependency tree... Reading state information... The following packages will be upgraded: file libmagic1 2 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. Need to get 0 B/256 kB of archives. After this operation, 110 kB disk space will be freed.
正如你所看到的,软件包“file”和“libmagic1”应该在第一天升级。 但他们不是。 所以第二天,他们再次提到。 但没有升级。
而今天,如果我跑步
$ sudo apt-get -y upgrade
包“文件”和“libmagic1”再次被提及,他们得到升级(最后)。
所以,正如你所看到的,我可以手动升级。 但是这些软件包应该在cron任务运行之前就已经升级了。
有关这个奥秘的线索吗?
加法星期六11月15日11:48:
这是什么出现在我的/var/log/apt/history.log周围的cron作业的时间。
Start-Date: 2014-11-13 15:52:03 Commandline: apt-get -y upgrade Upgrade: file:amd64 (5.11-2+deb7u5, 5.11-2+deb7u6), libmagic1:amd64 (5.11-2+deb7u5, 5.11-2+deb7u6) Error: Sub-process /usr/bin/dpkg returned an error code (2) End-Date: 2014-11-13 15:52:03 Start-Date: 2014-11-14 15:52:03 Commandline: apt-get -y upgrade Upgrade: file:amd64 (5.11-2+deb7u5, 5.11-2+deb7u6), libmagic1:amd64 (5.11-2+deb7u5, 5.11-2+deb7u6) Error: Sub-process /usr/bin/dpkg returned an error code (2) End-Date: 2014-11-14 15:52:03
不同软件包的/var/log/apt/history.log中会出现同样的消息。 例如,本月早些时候,“wget”包需要一些更新(我这次尝试了-qq选项,但这个选项似乎没有什么区别)。
Start-Date: 2014-11-03 15:52:02 Commandline: apt-get -y -qq upgrade Upgrade: wget:amd64 (1.13.4-3+deb7u1, 1.13.4-3+deb7u2) Error: Sub-process /usr/bin/dpkg returned an error code (2) End-Date: 2014-11-03 15:52:02 Start-Date: 2014-11-04 15:52:02 Commandline: apt-get -y -qq upgrade Upgrade: wget:amd64 (1.13.4-3+deb7u1, 1.13.4-3+deb7u2) Error: Sub-process /usr/bin/dpkg returned an error code (2) End-Date: 2014-11-04 15:52:03 Start-Date: 2014-11-05 15:52:03 Commandline: apt-get -y -qq upgrade Upgrade: wget:amd64 (1.13.4-3+deb7u1, 1.13.4-3+deb7u2) Error: Sub-process /usr/bin/dpkg returned an error code (2) End-Date: 2014-11-05 15:52:03
看来我find了问题的原因。
要检测错误,我必须在我的自定义日志中捕捉stderr。 事实certificate,这样做是有用的,因为一些错误没有邮寄到根目录,也没有写在其他日志中。
为了在日志中捕捉stderr,我首先将cron任务改为:
52 15 * * * apt-get -y upgrade >> /var/log/my-apt-upgrade.txt 2>&1
今天包“wlibgcrypt11”将被更新。 这一次,我的日志发现了一个错误。 比之前在/var/log/apt/history.log中出现的模糊错误信息更明确。
在今天的/var/log/my-apt-upgrade.txt中:
Reading package lists... Building dependency tree... Reading state information... The following packages will be upgraded: libgcrypt11 debconf: unable to initialize frontend: Dialog debconf: (TERM is not set, so the dialog frontend is not usable.) debconf: falling back to frontend: Readline debconf: unable to initialize frontend: Readline debconf: (This frontend requires a controlling tty.) debconf: falling back to frontend: Teletype dpkg-preconfigure: unable to re-open stdin: 1 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. Need to get 0 B/300 kB of archives. After this operation, 35.8 kB of additional disk space will be used. dpkg: warning: 'ldconfig' not found in PATH or not executable dpkg: warning: 'start-stop-daemon' not found in PATH or not executable dpkg: error: 2 expected programs not found in PATH or not executable Note: root's PATH should usually contain /usr/local/sbin, /usr/sbin and /sbin E: Sub-process /usr/bin/dpkg returned an error code (2)
所以这是一个PATH错误。
我系统上的根PATH包含所有必需的目录。 那么visudo中的secure_pathvariables也是如此。 这就是为什么当我手动运行sudo apt-get时,一切正常。
但是cron不设置环境variables。 所以我为每个cron任务添加了一个PATH环境variables。
$ sudo crontab -e 22 16 * * * PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' apt-get -y update >> /var/log/my-new-apt-update.txt 2>&1 32 16 * * * PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' apt-get -y upgrade >> /var/log/my-new-apt-upgrade.txt 2>&1
它的工作! 该软件包已成功由cron任务更新。