28

I have about 7 Debian servers I manage, and I would like to set them to automatically update themselves. So, I created a script as such:

#!/bin/sh
apt-get update
apt-get upgrade

and placed it on the root's crontab list. Unfortunately, it always hangs on the Upgrade section, asking if I'm sure I want to upgrade. Because it's a cron job, I don't see the output until it emails me saying it's failed. Is there a way to have it skip that prompt, and just do the upgrade automatically?

  • 3
    ... or cron-apt. – derobert Dec 30 '13 at 20:02
46

Use the -y option to apt-get to have it not ask. From man apt-get:

   -y, --yes, --assume-yes
       Automatic yes to prompts; assume "yes" as answer to all prompts and
       run non-interactively. If an undesirable situation, such as
       changing a held package, trying to install a unauthenticated
       package or removing an essential package occurs then apt-get will
       abort. Configuration Item: APT::Get::Assume-Yes.

You can also set the DEBIAN_FRONTEND env variable

DEBIAN_FRONTEND=noninteractive apt-get -y upgrade
  • 1
    What does DEBIAN_FRONTEND do? Is it used for other processes as well? – Canadian Luke REINSTATE MONICA Dec 30 '13 at 19:58
  • I'm trying this on my home server, and will choose the best answer as soon as it runs – Canadian Luke REINSTATE MONICA Dec 30 '13 at 20:04
  • 1
    @CanadianLuke see here for DEBIAN_FRONTEND. It's not mentioned in my Debian's man debconf though, so it may be an Ubuntu thing. – terdon Dec 30 '13 at 20:31
  • @terdon you don't have the -doc package for debconf. It's in the 7 section of the man man 7 debconf ;) – Braiam Dec 30 '13 at 20:48
  • @Braiam ah, OK, I saw that and tried man 7 debconf but got nothing. Now I know why :) – terdon Dec 30 '13 at 20:51
27

Well, maybe you are using the wrong tool. unattended-upgrades package installs security upgrades in daily basis (can be configured), you can configure what packages to upgrade or not upgrade, etc. Can be installed using:

sudo apt-get install unattended-upgrades

From man unattended-upgrades:

The configuration is done via the apt configuration mechanism. The default configuration file can be found at /etc/apt/apt.conf.d/50unattended-upgrades

  • @CanadianLuke it reads all the configurations in /etc/apt/apt.conf.d/ but only those starting with Unattended-Upgrade:: get parsed. – Braiam Dec 30 '13 at 20:04
  • I am trying this on one of the servers at work, and will choose the best answer as soon as it runs – Canadian Luke REINSTATE MONICA Dec 30 '13 at 20:05
10

while the previous answers are informative they don't circumvent the 'issue' of input required by human means during upgrade. therefore, i am using the following:

export DEBIAN_FRONTEND=noninteractive
export DEBIAN_PRIORITY=critical
sudo -E apt-get -qy update
sudo -E apt-get -qy -o "Dpkg::Options::=--force-confdef" -o "Dpkg::Options::=--force-confold" upgrade
sudo -E apt-get -qy autoclean

to include 'distribution' upgrades like kernels use the dist-upgrade command.

please see the manpgage of dpkg for in-depth information on these parameters.

importat note: calling sudo including the -E parameter is required:

Indicates to the security policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the user does not have permission to preserve the environment.

otherwise, the EXPORT statements will not affect the calls of apt-get!

credit goes to Remy van Elst! thanks!

  • 1
    Could you explain why you added the other options to apt-get upgrade? – FarO Dec 23 '17 at 11:25
  • 1
    Also, is "sudo -E" required if the script is run from rott's crontab? – FarO Dec 23 '17 at 11:26
  • 1
    @FarO that depends on what environment/in which context you would like the cronjob to run. generally, these are run by root - so you do not need to use sudo at all. the 'other options' are set to run unattended in any case. please see the referenced manpage. – jitter Jul 4 '18 at 8:09
4

A generic tool for this kind of thing is yes:

DESCRIPTION
       Repeatedly output a line with all specified STRING(s), or 'y'.

So, for example, you could do

yes | sudo apt-get upgrade 

Please note that in the specific case of apt-get upgrade using the options suggested by @Braiam or @ArthurUlfeldt is better.

  • The line I paste when I want to do it manually is apt-get update && yes | apt-get upgrade (our servers aren't supposed to use sudo... Don't ask...) – Canadian Luke REINSTATE MONICA Aug 14 '14 at 18:54
  • Why using a trick instead of an option already provided?? "-y" is in apt-get already. – FarO Dec 21 '17 at 10:42
  • 3
    Because, as I said in the answer, this is a generic tool for this sort of operation, so this answer is for other cases, not for apt. Did you not read the last paragraph? Or the first sentence? – terdon Dec 21 '17 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.