53

I have an unattended script for installing servers. At the beginning of the script there is a sudo apt-get dist-upgrade --yes. The dist upgrade has a nasty user input screen at its end asking to restart services:
enter image description here

Is it possible to autoaccept service restarts or disable this screen? It breaks my whole script. Also Im afraid it might leave my server stuck at some point when updating...

same result with apt-get upgrade

edit: I tried without success:

#!/bin/bash
sudo apt-get update
sudo apt-get remove apt-listchanges --assume-yes --force-yes &&

#using export is important since some of the commands in the script will fire in a subshell export DEBIAN_FRONTEND=noninteractive && export APT_LISTCHANGES_FRONTEND=none &&

#lib6c was an issue for me as it ignored the DEBIAN_FRONTEND environment variable and fired a prompt anyway. This should fix it echo 'libc6 libraries/restart-without-asking boolean true' | debconf-set-selections &&

echo "executing wheezy to jessie" && find /etc/apt -name "*.list" | xargs sed -i '/^deb/s/wheezy/jessie/g' &&

echo "executing autoremove" && sudo apt-get -fuy --force-yes autoremove &&

echo "executing clean" && sudo apt-get --force-yes clean &&

echo "executing update" && sudo apt-get update &&

echo "executing upgrade" && sudo apt-get --force-yes -o Dpkg::Options::="--force-confold" --force-yes -o Dpkg::Options::="--force-confdef" -fuyq upgrade &&

echo "executing dist-upgrade" && sudo apt-get --force-yes -o Dpkg::Options::="--force-confold" --force-yes -o Dpkg::Options::="--force-confdef" -fuyq dist-upgrade

julian bechtold
  • 785
  • 1
  • 7
  • 17

10 Answers10

53

Other answers just skipped needrestart altogether.

But the environment variable NEEDRESTART_MODE allows to specify a mode. And by choosing "(a)utomatically", you can benefit from needrestart without being blocked by the prompt:

sudo NEEDRESTART_MODE=a apt-get dist-upgrade --yes
PowerKiKi
  • 954
39

As others mentioned, the trouble in this case is with the needrestart command, which is part of the apt-get upgrade process in Ubuntu now (specifically 22.04 which is what I am using). By default this is set to "interactive" mode which causes the interruption of scripts.

To change this behavior, we can edit the /etc/needrestart/needrestart.conf file, changing the line:

#$nrconf{restart} = 'i';

to

$nrconf{restart} = 'a'; (if we want to restart the services automatically) or $nrconf{restart} = 'l'; to simply list the services that need restart.

If you are running a script and want to make this edit without using an interactive editor like vim, you can do so using sed, something like:

sudo sed -i 's/#$nrconf{restart} = '"'"'i'"'"';/$nrconf{restart} = '"'"'a'"'"';/g' /etc/needrestart/needrestart.conf

It looks ugly because of the single quotes in the config file and how sed handles single quotes, but it does work. Please leave a comment if you have a better looking approach.

16

Instead of using sed or editing the main config, add your own config file in /etc/needrestart/conf.d/no-prompt.conf:

$nrconf{restart} = 'a';
Joey Parrish
  • 161
  • 1
  • 2
11

Alternatively, I think you can remove the needrestart package itself.

sudo apt-get remove needrestart

I am using this for AWS EC2 provisioning on Ubuntu 22.04. For whatever reason, export DEBIAN_FRONTEND=noninteractive did not seem to work.

9

Vardogor's answer worked for me, just with a tiny difference, one dash (-y) and not two (--y):

sudo DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade -y

It gives this error if I use two dashes:

E: Command line option --y is not understood in combination with the other options

I'm doing it on Ubuntu 22.04 LTS

Lascou
  • 191
4

Use:

sudo NEEDRESTART_SUSPEND=1 apt-get dist-upgrade --yes
turbo
  • 66
2

An alternative to Joe's answer without sed is the below,

echo "\$nrconf{restart} = \"l\"" | sudo tee -a /etc/needrestart/needrestart.conf

This will add a line to /etc/needrestart/needrestart.conf that looks like the following, $nrconf{restart} = "l"

This also seems to achieve the desired functionality and stops the interactive pop-up. I had problems with the sed solution when trying to pass it into another shell specifically when using multipass with multipass exec test -- sed-command probably because of all the quotes. The echo solution above is a little easier and if anyone needs to use it with something like multipass you can via,

multipass exec name -- sh -c 'echo "\$nrconf{restart} = \"l\"" | sudo tee -a /etc/needrestart/needrestart.conf'

Just replace name with the name of your vm.

User31415
  • 21
  • 1
0

Try sudo DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade --y, it should prevent any of these prompts

Vardogor
  • 468
0

To add to the edit the /etc/needrestart/needrestart.conf file from JoeShabadu2000 above - you can create a file containing the configuration value to set in /etc/needrestart/conf.d/. All .conf files in that directory are processed. See the README in the folder. Confirmed working in Ubuntu 22.04.

Example to set to list vs interactive:

sudo tee "/etc/needrestart/conf.d/mynrconf.conf" > /dev/null << SHIRT
\$nrconf{restart} = 'l'
SHIRT
0

*answer partially taken from other answers here

Run this once

[ -d /etc/needrestart/conf.d ] && echo "\$nrconf{restart} = 'a';" | sudo tee /etc/needrestart/conf.d/zzz-no-restart.conf > /dev/null

This creates a new config file to make the restarts automatic:

zzz in file name makes sure it gets highest priority over any existing configs.

How it works

Files ending with .conf and located in the /etc/needrestart/conf.d directory are parsed by needrestart's default configuration file.

Files are parsed in order (using Perl's sort sub) and override or modify any previously set config option.

penduDev
  • 101
  • 1