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

我正在尝试使用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? 看起来它应该非常简单,而且更加流畅。

crontab的

 root@daedalus:~/bin# crontab -l # mh dom mon dow command 45 06 * * * ~/bin/upgrades.sh 

upgrades.sh

 root@daedalus:~/bin# cat upgrades.sh #!/bin/bash /usr/bin/apt-get -y update /usr/bin/aptitude -y safe-upgrade 

错误

 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... 

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

尝试添加

 PATH=/usr/bin:/bin:/usr/sbin:/sbin 

到你的crontab的顶部。

或者您可以将相同的PATH行放在~/bin/upgrades.sh的第二行。 这样,来自命令行的测试和来自crontab的测试应该产生相同的结果。

虽然您的主要问题已经得到解决,但看起来您正在收到debconf警告,因为您在没有交互式tty的情况下运行apt-get。 要删除这些消息,可以设置此环境变量:

 DEBIAN_FRONTEND=noninteractive 

有一个社区维基页面: crontab不起作用的原因 。 在这种情况下,似乎原因是cron的有限环境变量。 你能从/etc/cron.daily中的shell脚本运行吗?

来自CronHowto :

根据正在运行的命令,您可能需要通过将以下行放在其crontab文件的顶部来扩展root用户PATH变量:

PATH = / usr / sbin目录:在/ usr / bin中:/ sbin目录:/ bin中

但事实上,一切似乎都和你一样好……你从哪里获取这些错误?

CRONTAB:

 root@PORTATIL:/var/log$ crontab -l * * */2 * * /usr/share/myupdate.sh > /var/log/myupdate.log 

脚本:

 root@PORTATIL:/etc# cat /usr/share/myupdate.sh #!/bin/bash #Testing updates apt-get update -y apt-get upgrade -y 

日志:

 root@PORTATIL:/etc# cat /var/log/myupdate.log Hit http://security.ubuntu.com lucid-security Release.gpg Hit http://archive.canonical.com lucid Release.gpg Hit http://archive.canonical.com lucid Release.gpg Hit http://packages.medibuntu.org lucid Release.gpg Get:1 http://dl.google.com stable Release.gpg [197B] Hit http://ppa.launchpad.net lucid Release.gpg Hit http://ppa.launchpad.net lucid Release.gpg Hit http://ppa.launchpad.net lucid Release.gpg Hit http://security.ubuntu.com lucid-security Release Hit http://badgerports.org lucid Release.gpg Hit http://archive.canonical.com lucid Release Hit http://ppa.launchpad.net lucid Release.gpg Hit http://ppa.launchpad.net maverick Release.gpg Hit http://ppa.launchpad.net lucid Release.gpg Hit http://ppa.launchpad.net lucid Release.gpg Hit http://ppa.launchpad.net lucid Release.gpg Get:2 http://dl.google.com stable Release [1347B] Hit http://security.ubuntu.com lucid-security/main Packages Hit http://ppa.launchpad.net lucid Release Hit http://ppa.launchpad.net lucid Release Hit http://packages.medibuntu.org lucid Release Hit http://download.virtualbox.org lucid Release.gpg Hit http://archive.canonical.com lucid Release Hit http://linux.dropbox.com lucid Release.gpg Get:3 http://dl.google.com stable/main Packages [1110B] Hit http://security.ubuntu.com lucid-security/restricted Packages Hit http://security.ubuntu.com lucid-security/main Sources Hit http://security.ubuntu.com lucid-security/restricted Sources Hit http://security.ubuntu.com lucid-security/universe Packages Hit http://security.ubuntu.com lucid-security/universe Sources Hit http://ppa.launchpad.net lucid Release Hit http://ppa.launchpad.net lucid Release Hit http://ppa.launchpad.net maverick Release Hit http://ppa.launchpad.net lucid Release Hit http://ppa.launchpad.net lucid Release Hit http://badgerports.org lucid Release Hit http://archive.canonical.com lucid/partner Packages Hit http://security.ubuntu.com lucid-security/multiverse Packages Hit http://security.ubuntu.com lucid-security/multiverse Sources Hit http://ppa.launchpad.net lucid Release Hit http://packages.medibuntu.org lucid/free Packages Hit http://download.virtualbox.org lucid Release Hit http://es.archive.ubuntu.com lucid Release.gpg Hit http://linux.dropbox.com lucid Release Hit http://archive.canonical.com lucid/partner Packages Hit http://ppa.launchpad.net lucid/main Packages Hit http://ppa.launchpad.net lucid/main Packages Hit http://es.archive.ubuntu.com lucid-updates Release.gpg Hit http://badgerports.org lucid/main Packages Hit http://ppa.launchpad.net lucid/main Packages Hit http://ppa.launchpad.net lucid/main Packages Hit http://ppa.launchpad.net maverick/main Packages Hit http://ppa.launchpad.net lucid/main Packages Hit http://ppa.launchpad.net lucid/main Packages Hit http://packages.medibuntu.org lucid/non-free Packages Hit http://linux.dropbox.com lucid/main Packages Hit http://es.archive.ubuntu.com lucid Release Hit http://download.virtualbox.org lucid/contrib Packages Hit http://ppa.launchpad.net lucid/main Packages Hit http://es.archive.ubuntu.com lucid-updates Release Hit http://es.archive.ubuntu.com lucid/main Packages Hit http://es.archive.ubuntu.com lucid/restricted Packages Hit http://es.archive.ubuntu.com lucid/main Sources Hit http://es.archive.ubuntu.com lucid/restricted Sources Hit http://es.archive.ubuntu.com lucid/universe Packages Hit http://es.archive.ubuntu.com lucid/universe Sources Hit http://es.archive.ubuntu.com lucid/multiverse Packages Hit http://es.archive.ubuntu.com lucid/multiverse Sources Hit http://es.archive.ubuntu.com lucid-updates/main Packages Hit http://es.archive.ubuntu.com lucid-updates/restricted Packages Hit http://es.archive.ubuntu.com lucid-updates/main Sources Hit http://es.archive.ubuntu.com lucid-updates/restricted Sources Hit http://es.archive.ubuntu.com lucid-updates/universe Packages Hit http://es.archive.ubuntu.com lucid-updates/universe Sources Hit http://es.archive.ubuntu.com lucid-updates/multiverse Packages Hit http://es.archive.ubuntu.com lucid-updates/multiverse Sources Fetched 2654B in 1s (1628B/s) Reading package lists... Reading package lists... Building dependency tree... Reading state information... 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.