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


15

我正在尝试使用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。

Crontab

root@daedalus:~/bin# crontab -l
# m h  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...

Answers:


10

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

尝试添加

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

到您的顶部crontab

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


我知道了。我将命令放入脚本中,错误地认为#!/ bin / bash行会加载root的正常路径。显然我错了。那么首先确定用户默认路径的是什么,以及如何应用它呢?
djeikyb 2011年

cron为什么不采用用户路径?将路径添加到crontab或我的脚本会更好吗?有缺点吗?
djeikyb 2011年

应该是出于安全原因,但是我同意,这很烦人。1)如果将其放在脚本中,则可以放入PATH=...一个文件(例如)~/.env,并使用. ~/.env靠近脚本顶部的位置从编写的每个脚本中获取它。然后,如果您更改了内容PATH,则只需编辑一个文件。2)如果将其放在中crontab,则意味着您不必编辑所有的cron脚本,但是如果要更改您的代码PATH(例如~/.bashrccrontab),将有两个位置可以编辑。哪个更好取决于您。
Mikel,

凉。我想我的问题是,为什么它不安全?我现在正在谷歌搜索,但还没有提出任何建议。无论如何,我已经进行了更改,明天运行时,我会标记为已回答,以防万一其他事情变了样。
djeikyb 2011年

我希望我也知道。如果有原因,则文档或cron源代码都不会说明原因。从理论上讲,本可以强制使用一致的环境,以便您可以将crontab从一个用户复制到另一个用户,但是只能PATH进行更改,所以这不是原因。
Mikel,

14

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

DEBIAN_FRONTEND=noninteractive


0

CronHowto

根据正在运行的命令,您可能需要通过在root用户的crontab文件顶部放置以下行来扩展root用户的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.

我的错误来自邮寄给我的Cron作业日志。我正在运行10.04,与您相同。奇怪..
djeikyb

这里一定是一些cron配置文件,用于设置限制性路径...我现在没有问题。
luri 2011年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.