该服务器运行 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
包“file”和“libmagic1”再次被提及并且它们最终被升级。
因此,如您所见,我可以手动升级。但是这些软件包应该在 cron 任务运行时就升级了。
关于这个谜团有什么线索吗?
添加于 11 月 15 日星期六 11:48:
这是 cron 作业执行期间我的 /var/log/apt/history.log 中出现的内容。
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
答案1
我似乎找到问题的原因了。
为了检测错误,我必须在自定义日志中捕获 stderr。事实证明这样做很有用,因为有些错误没有发送给 root,也没有写入其他日志中。
为了在我的日志中捕获 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_path 变量也是如此。这就是为什么当我手动运行 sudo apt-get 时一切都正常的原因。
但是 cron 没有设置环境变量。因此我为每个 cron 任务添加了一个 PATH 环境变量。
$ 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 任务成功更新。