Automatic Reboot at specific time if Automatic Upgrade requires Reboot

I have setup automatic updates and it updates automatically. However, there are updates that require a restart and I know

Unattended-Upgrade::Automatic-Reboot "true";

will set it to reboot. But it reboots as soon as it updates. That could be at any time of the day, when my users are using my server. I want it to restart at a specific time, say 12:00 am that night. How do I do that?


Have a look in /etc/apt/apt.conf.d/50unattended-upgrades. You want Unattended-Upgrade::Automatic-Reboot-Time:

// If automatic reboot is enabled and needed, reboot at the specific
// time instead of immediately
//  Default: "now"
//Unattended-Upgrade::Automatic-Reboot-Time "02:00";

You can disable automatic reboot by setting in your configuration file :

Unattended-Upgrade::Automatic-Reboot "false"

Reconfigure it :

dpkg-reconfigure -plow unattended-upgrades

Write a script to check whether a reboot is required :

#!/bin/bash
if [ -f /var/run/reboot-required ]; then 
/sbin/reboot now 
fi

Set the correct permissions on the script :

chmod +x <script_name>.sh

Set a cron job so it will be executed everyday at 12 am.

crontab -e 

Then append the above :

0 0 * * * <path_to_your_script>.sh

Save it and it's done.