为什么 apt-get 在使用 cron 自动化时失败?

dje*_*kyb 16 aptitude apt cron

我正在尝试使用 cron 来自动化我的系统更新。您可以在下面看到我的 crontab、命令和由此产生的错误。

当我以 root 身份运行 upgrades.sh 时,脚本运行良好。当 cron 运行它时,apt-get -y update运行没有问题,但aptitude -y safe-upgrade失败了。我猜这个错误:debconf: (This frontend requires a controlling tty.)是因为有一个内核更新,它反过来更新 grub,这需要我明确说可以覆盖/boot/grub/menu.lst. 但我不明白路径错误。我想要不需要我监督的更新。

我已经通读了这个问题,它是尚未被接受的解决方案unattended-upgrades,我可能最终会使用它,但为什么我不能使用 cron?看起来它应该非常简单,而且更加 linuxy。

定时任务表

root@daedalus:~/bin# crontab -l
# m h  dom mon dow   command
45 06 * * * ~/bin/upgrades.sh
Run Code Online (Sandbox Code Playgroud)

升级.sh

root@daedalus:~/bin# cat upgrades.sh 
#!/bin/bash
/usr/bin/apt-get -y update
/usr/bin/aptitude -y safe-upgrade
Run Code Online (Sandbox Code Playgroud)

错误

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:
Fetched 37.6MB in 4min 23s (143kB/s)
dpkg: warning: 'ldconfig' not found on PATH.
dpkg: warning: 'start-stop-daemon' not found on PATH.
dpkg: warning: 'update-rc.d' not found on PATH.
dpkg: 3 expected program(s) not found on PATH.
NB: root's PATH should usually contain /usr/local/sbin, /usr/sbin and /sbin.
E: Sub-process /usr/bin/dpkg returned an error code (2)
A package failed to install.  Trying to recover:
dpkg: warning: 'ldconfig' not found on PATH.
dpkg: warning: 'start-stop-daemon' not found on PATH.
dpkg: warning: 'update-rc.d' not found on PATH.
dpkg: 3 expected program(s) not found on PATH.
NB: root's PATH should usually contain /usr/local/sbin, /usr/sbin and /sbin.
Reading package lists...
Building dependency tree...
Reading state information...
Reading extended state information...
Initializing package states...
Writing extended state information...
Run Code Online (Sandbox Code Playgroud)

Tri*_*onX 15

尽管您的主要问题已经得到解答,但您似乎收到了 debconf 警告,因为您在没有交互式 tty 的情况下运行 apt-get。要摆脱这些消息,您可以设置此环境变量:

DEBIAN_FRONTEND=noninteractive
Run Code Online (Sandbox Code Playgroud)


Mik*_*kel 10

这些消息告诉您您的PATH环境变量是错误的。

尝试添加

PATH=/usr/bin:/bin:/usr/sbin:/sbin
Run Code Online (Sandbox Code Playgroud)

到您的crontab.

或者你可以把同一PATH行的第二行~/bin/upgrades.sh。这样你的命令行测试和你的测试crontab应该产生相同的结果。