First, you will need to edit your crontab file. Simply type
sudo crontab -e
in a terminal. You will be asked which editor you wish to use. I choose vim simply because I like it, but you will have other choices, including the gedit graphical editor (in Ubuntu, anyway). Once the crontab file appears in your editor, just type
0 0 * * 0 root (apt-get update && apt-get -y -d upgrade) > /dev/null
and save it. That's it- the cronjob will run at midnight every night. If you want to specify a different time/date, just replace the first
0with the minute (0-59), the second
0with the hour (0-23), and the last
0with the three-letter day (sun, mon, tue, etc.). The day of month and month can be specified by replacing the asterisks with the proper numbers- 1-31 for the first asterisk and 1-12 for the second. The
rootpart just tells cron to run the following commands as root. The
-yoption tells cron to automatically answer 'yes' to the prompts, so it doesn't just hang waiting for your response. The
-doption tells it to just download the files, but not install them until you tell it. This option can be removed, but it's not very safe- you always risk the very slim possibility that the update will cause something to stop working. So review the list of packages to be installed before you actually install them. Lastly, the
> /dev/nullredirect tells cron to direct the output to the black hole that is /dev/null, keeping your terminal free of lengthy output.
There are, of course, various other commands and options for using cron. Simply type
cron --helpfor a manual or list of commands. And enjoy your up-to-date Linux distribution!