44

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
1
  • 2
    See man needrestart. There are several options that seem to fit your need.
    – user535733
    Commented Oct 3, 2021 at 13:53

9 Answers 9

43

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
4
  • 1
    I feel like this should be the top answer because you seem to have done it most directly. Commented Oct 18, 2022 at 16:53
  • I did this and I thought it worked but the pop up came back. Should this be in .bashrc or how do we make sure it’s a permanent option? Commented Oct 20, 2022 at 13:54
  • 4
    This is not a permanent solution. On the contrary it is meant to be temporary, without editing any configuration file. If you want to make it permanent, you can wrap it in a custom script, or see other solutions that edit configuration files.
    – PowerKiKi
    Commented Oct 20, 2022 at 14:26
  • If it needs to be permanent you can just add it to .bashrc like any other environment variable (assuming normal login shell). This inlined variable answer is clean IMO in that it keeps the environment uncluttered and just issues the mode variable when it's required by the scripting.
    – bsplosion
    Commented Feb 13 at 19:13
33

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.

5
  • Somehow the sed command gave me $nrconf$nrconf{restart} = 'l';
    – Frexuz
    Commented Aug 22, 2022 at 7:13
  • 5
    the easiest way to do this is (as per the manuals) you set the installation flag to be non interactive. e.g., DEBIAN_FRONTEND=noninteractive So in my scripts I export DEBIAN_FRONTEND=noninteractive at the start and it behaves "normally" i.e. it doesn't ask for input
    – Luke West
    Commented Sep 29, 2022 at 12:58
  • 5
    Instead of the "quote hell" sed you can replace with double quotes: sed -i "s/#\$nrconf{restart} = 'i';/\$nrconf{restart} = 'a';/" /etc/needrestart/needrestart.conf
    – Talisman
    Commented Jan 23, 2023 at 11:38
  • sed itself doesn't do anything special with single quotes, it's the shell that does. I would recommend fixing based on Talisman's suggestion, or at least replacing "'"'i'"'" with "'i'" or \'i\'.
    – mwfearnley
    Commented Nov 28, 2023 at 9:50
  • 1
    Better add your own file in /etc/needrestart/conf.d/ as suggested by another answer: askubuntu.com/a/1444549/269499
    – Fritz
    Commented Feb 16 at 10:09
12

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';
1
  • 3
    This is proper answer. Editing package-shipped configs will result in upgrade conflict some day.
    – PeterM
    Commented Jan 15 at 17:11
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

2
  • 2
    easier to export DEBIAN_FRONTEND=noninteractive at the start of the script
    – Luke West
    Commented Sep 29, 2022 at 13:00
  • Could you explain this? You restart the daemons that need restarting with dist-upgrade, but do so non-interactively by temporarily setting that variable? But is that a one-time restart or a persistent setting? The end goal would be to never have to see that pop-up menu going forward. Commented Oct 20, 2022 at 13:58
8

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.

1
  • 1
    Yes, this may not be perfect but the only solution worked for me on AWS EC2 instances.
    – sarat
    Commented Feb 6, 2023 at 19:34
4

Use:

sudo NEEDRESTART_SUSPEND=1 apt-get dist-upgrade --yes
0
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.

0

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

1
  • 3
    This does not solve the problem, i have been using this for eon's and the prompt in 22.04 is different. try the other solutions.
    – ekydfejj
    Commented Aug 29, 2022 at 19:44
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .