Automatically Update Ubuntu Server And Restart If Needed

less than 1 minute read

Create a shell script

Save file as /home/<yourusername>/restart_when_needed.sh Script checks if restart is required and reboots system if required.

#!/bin/bash

if [ -s /var/run/reboot-required ]; then echo "REBOOTING!" && reboot; else echo "NO REBOOT REQUIRED"; fi

Add tasks to CRON

Add lines to /etc/crontab (sudo/root privileges needed)

30 23   * * *   root    apt update && apt upgrade -y
45 23   * * *   root    /home/<yourusername>/restart_when_needed.sh

This will (everyday):

  • update your system at 23:30 (11:30 PM)
  • run the script at 23:45 (11:45 PM)

Updated: