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
Inside the file look for the line:
and make it look like:
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
if [ -f /var/run/reboot-required ]; then
echo $(date) Sytem restart required by: $(cat /var/run/reboot-required.pkgs)
Save this file as
Make this file executable:
sudo chmod +x
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.
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.
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