Skip to main content

Ubuntu 22 Apt Auto-Restart Services

Permanently Enabling Auto-Restart

The trouble is with the needrestart command, which is part of the apt-get upgrade process in Ubuntu now (specifically 22.04). 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

sudo nano /etc/needrestart/needrestart.conf

Change the line:

#$nrconf{restart} = 'i';

to

$nrconf{restart} = 'a';

And to fix the Kernel module prompt, change the line:

#$nrconf{kernelhints} = -1;

to

$nrconf{kernelhints} = -1;

Or the easy way to do both is to run the following command:

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

Enabling Auto-Restart for a single upgrade in a script

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