How can I write a script in Ubuntu such that it download updates at night when I sleep and reboots the computer if it needs a reboot?
sudo apt install unattended-upgrades
will install it for you.
Installing it should be sufficient to enable it. To allow it to reboot when necessary, you’ll have to add a configuration file, e.g.
By default, on Ubuntu, it will install any upgrade from the main repositories, whether it’s security-related or not. You can configure more repositories if necessary by overriding the
Unattended-Upgrade::Allowed-Origins configuration key.