Stop Unattended-Upgrades from restarting the computer
Right now Unattended-Upgrades is setup to restart the computer at 2:00AM if the update requires a restart. This step will stop this behavior.
Step: edit the file /etc/apt/apt.conf.d/50unattended-upgrades
Inside the file look for the line:
unattended-Upgrade::Automatic-Reboot "true";
and make it look like:
Unattended-Upgrade::Automatic-Reboot "false";
Create a bash script to check if restart is required and do it if needed
The script below checks if the file reboot-required
exists and if so, it reboots the computer immediately. Let's call this file reboot_if_needed.sh
.
#!/bin/bash
if [ -f /var/run/reboot-required ]; then
echo $(date) Sytem restart required by: $(cat /var/run/reboot-required.pkgs)
/sbin/reboot now
fi
Save this file as /opt/bin/reboot_if_needed.sh
Make this file executable:
sudo chmod +x /opt/bin/reboot_if_needed.sh
Explanation
When the script runs and the file var/run/reboot-required
exists it will output some text that will be captured in a log file /var/log/reboot_history.log
. Then the system will restart.
Schedule reboot_if_needed.sh
every night at 2:00 AM
We want to run the script with administrator privileges. So we use sudo
. Open a terminal by pressing Ctrl+Alt+T and enter:
sudo crontab -e
This command will open the crontab file for the root user if one exists, or create a blank new file. Add the following line at the end of the file:
0 02 * * * /opt/bin/reboot_if_needed.sh >> /var/log/reboot_history.log
If you use nano
as the text file editor, exit the editor by pressing Ctrl+X. The editor will prompt you to save the changes. Press Y and then press Enter to select the default file name.
Explanation
Each line in crontab
has five time-and-date fields, followed by a command, followed by a newline character ('\n'). The fields are separated by spaces. The five time-and-date fields cannot contain spaces. The five time-and-date fields are as follows:
- minute (0-59),
- hour (0-23, 0 = midnight),
- day (1-31),
- month (1-12),
- weekday (0-6, 0 = Sunday).
Therefore, 0 02 * * *
means at 2:00 AM every night.
The line added to the crontab
will make the script reboot_if_needed.sh
run every night at 2 AM. The >>
redirects the output to the /var/log/reboot_history.log
file. This file will be created the first time crontab
runs the script.
See https://help.ubuntu.com/community/CronHowto for how to use crontab
Why this works?
Unattended-Upgrade apparently schedules the restart with a delay. For example:
sudo shutdown -r 02:00
The above command schedules the restart at 2 AM. When such a command is in effect you get the message "another shutdown already pending".
We use crontab to take care of the starting the restart process at 2 AM, instead of scheduling it from within the shutdown
command. Thus, there is no shutdown pending once Unattended-Upgrade has run. Any user can reboot the computer.
When any user reboots the computer the file /var/run/reboot-required
if it exits is deleted by the system as reboot is not required any more. The computer will not be restarted at 2 AM by the script.
Hope this helps