Preventing Apt Upgrade from Restarting Systemd Services
When shutting down or rebooting a server, typically packages and the kernel are upgraded by issuing shutdown or reboot commands. However, Apt's behavior is to restart services that are being upgraded, which can be disruptive and potentially cause downtime or data loss.
Understanding the Issue
By default, Apt will automatically restart any services that are being upgraded. While this behavior can be useful for ensuring that the latest security updates are applied, it can also cause issues if the service in question is critical to the operation of the server.
For example, if a database service is being upgraded and is automatically restarted, any active connections to the database will be dropped, potentially causing data loss or corruption. Similarly, if a web server is being upgraded and is restarted, any active web requests will be dropped, causing errors and potentially impacting users.
Preventing Automatic Restarts
To prevent Apt from automatically restarting services during an upgrade, you can use the --no-start option as follows:
sudo apt-get upgrade --no-startThis will install the upgrades without restarting any services. However, this can leave the system in an inconsistent state, with outdated services that are no longer compatible with the new versions of the packages that have been installed.
Manually Restarting Services
Once the upgrade is complete, you can manually restart any services that were upgraded using the systemctl command. For example, to restart the Apache web server, you can use the following command:
sudo systemctl restart apache2By manually restarting services, you can ensure that any active connections or requests are handled gracefully, reducing the risk of data loss or corruption.
Persistently Disabling Automatic Restarts
If you want to persistently disable automatic restarts during upgrades, you can modify the Apt configuration file at /etc/apt/apt.conf.d/20auto-upgrades.
Add the following line to the file:
APT::Update::Post-Invoke {"/bin/systemctl disable --now <service-name>";};Replace <service-name> with the name of the service you want to disable automatic restarts for. This will disable the service and prevent it from being restarted during upgrades.
- By default, Apt will automatically restart services during an upgrade.
- To prevent automatic restarts during an upgrade, use the
--no-startoption. - Once the upgrade is complete, manually restart any services using the systemctl command.
- To persistently disable automatic restarts during upgrades, modify the Apt configuration file.