Currently, to get things up to date on my Raspberry Pi, I have to type in sudo apt-get update and sudo apt-get upgrade. Is there a way to set it up to automatically do this? I am running Raspbian Jessie.

up vote 40 down vote accepted

instead of a manual cron, you can install unattended-upgrades, which is useful to ensure the latest kernels are installed. It's mostly designed for security.

https://wiki.debian.org/UnattendedUpgrades

apt-get install unattended-upgrades

There are some bits that can be adjusted/configured, but the default is fine.

  • 5
    For me, the default was not fine. It didn't do anything: the /var/log/unattended-upgrades/unattended-upgrades.logfile reported Allowed origins are: []so basically it wasn't pulling any updates in. After updating /etc/apt/apt.conf.d/50unattended-upgrades to include "origin=Raspbian,codename=${distro_codename},label=Raspbian";updates started to come in. – Rabarberski May 18 '17 at 20:10
  • My bone stock (as of September 2017) 50unattended-upgrades lists "origin=Debian,codename=${distro_codename},label=Debian-Security"; and it appears to find relevant packages correctly. Of course it ignores many of them. I've not set up any white or blacklists. – Jon V Sep 15 '17 at 13:11
  • The default 50unattended-upgrades is indeed stupid and has to be changed for this to work on Raspian. There is no Raspian-Security. Security updates are not labeled. It is important to add both "origin=Raspbian,codename=${distro_codename},label=Raspbian"; and "origin=Raspberry Pi Foundation,codename=${distro_codename},label=Raspberry Pi Foundation"; to /etc/apt/apt.conf.d/50unattended-upgrades – user643011 Apr 8 at 0:37

In a terminal, type:

sudo su

To access a root-level prompt. Then, run:

crontab -e

To begin editing your crontab, a table of tasks to run automatically at a given time. It will open in your preferred text editor. Modify it to include the following lines:

0 7 * * 1 apt update && apt upgrade -y

To automatically update and upgrade your apps at 7AM every Monday. Read-up on crontab to see how to adjust to your liking.

  • 2
    it's probably better to put the commands on the same line with && since they are related and there's no reason to do 'upgrade' if 'update' fails. – tedder42 Dec 5 '15 at 2:36
  • in crontab 0 means Sunday but in your code you have put 1 so the command would be run on Monday, not Sunday – rav_kr Dec 5 '15 at 21:14
  • 3
    dist-upgrade is better than upgrade, you will end up with broken dependencies using only upgrade. You also won't get around updates that require dialog confirmations. @tedder42 pointed out the method preferred by Debian (using unattended-upgrades). – ryanpcmcquen Jan 31 '16 at 18:51
  • You missed the second sudo after adding the &&. Although it's in root crontab anyway. – user643011 Apr 12 at 11:41
  • 1
    There should not be any sudo in there; you've switched to the root account by using su. The superuser's crontab is run with superuser privileges. – goldilocks Apr 12 at 12:34

You can install and configure cron-apt. Install it by doing the following:

apt-get install cron-apt

the main configuration file is /etc/cron-apt/config

One thing I add to my configuration is:

MAILON="always"

this will send an email every time it runs, not only if it encounters an error.

Note that the default setup will not automatically install the updates (there are some good reasons to not do this), but you can config it to do so. One advantage this program provides over a simple cron solution is that it gives you control over what gets installed (only download new packages, install security updates or install all updates).

To set the time it runs edit the /etc/cron.d/cron-apt file.

You can find more info and config options here

The package includes very good documentation, however it is gzipped. To extract the file:

  1. create a directory to work in - mkdir cron-apt_documentation
  2. change to the new directory - cd cron-apt_documentation
  3. copy the compressed file - cp /usr/share/doc/cron-apt/README.gz . (note the trailing dot)
  4. uncompress the file - gunzip README.gz
  5. read the file - cat README | less

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.