apt-get升级将在命令行上运行,但不能从cron bash脚本运行

在命令行上运行以下命令可以正常工作

$sudo apt-get update $sudo apt-get upgrade Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be upgraded: linux-libc-dev 1 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. Need to get 0B/864kB of archives. After this operation, 0B of additional disk space will be used. Do you want to continue [Y/n]? 

但是使用cron运行这个脚本…

 #!/bin/bash source /home/adm/.profile apt-get update >> /home/adm/update_detailed.log apt-get --yes upgrade >> /home/adm/update_detailed.log echo "Update_successful $(date)" >> /home/adm/update.log 

产生以下输出:

 Reading package lists... Building dependency tree... Reading state information... The following packages will be upgraded: linux-libc-dev 1 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. Need to get 0B/864kB of archives. After this operation, 0B of additional disk space will be used. 

为什么linux-libc-dev软件包不是从bash脚本安装的,而是可以从命令行安装的? 请注意,该脚本设置为以超级用户身份运行。

该脚本经过了[1]和在线资源的validation。

我在serverfault上看到的问题提到无人值守的升级,但我想了解这个问题不使用替代。

在[1]中我已经读过,这类问题可能是由环境variables引起的。 这就是为什么我将source /home/adm/.profile行添加到脚本的原因。 这没有什么区别。

[1] Unix和Linux系统pipe理手册,4ed,0-13-148005-7

你不是第一个遇到这个问题,发现它几乎不可行的。 这就是为什么Debian为此目的已经有多年了。 它被命名为cron-apt

安装此软件包,然后通过编辑/etc/cron-apt目录中的configuration文件对其进行configuration。 默认configuration文件是非常好的文件,所有的选项应该相当好解释。

请注意,虽然您可以将其configuration为自动更新系统,但您可能不应该这样做,因为它最终会破坏重要的事情 。 更好的是只要将其设置为在更新可用时通过电子邮件发送给您。