» Schedule automatic updates on Ubuntu

Making sure your system is up to date is a key attribute to it's security. Furthermore Ubuntu releases updates pretty often and you probably don't want to miss out on added stability and features. You could run updated manually, but why not schedule the updates in the background to make sure you are always running the latest stable versions, without ever having to worry about it.

Crontab

The crontab command, found in Unix and Unix-like operating systems, is used to schedule commands to be executed periodically. To see what crontabs are currently running on your system, you can open a terminal and run:

sudo crontab -l

To edit the list of cronjobs you can run:

sudo crontab -e

This wil open a the default editor (could be vi or pico, if you want you can change the default editor) to let us manipulate the crontab. If you save and exit the editor, all your cronjobs are saved into crontab. Cronjobs are written in the following format:

* * * * * /bin/execute/this/script.sh

If you want to know more about crontab, I've written another article: Schedule tasks on Linux using crontab

Updating with aptitude

I always used apt-get to update systems but I found out that aptitude has better dependency solving capabilities. So lets also use aptitude for this, it comes preinstalled. Normally I would run something like this from a terminal:

aptitude update # gets information on the latest packages
aptitude dist-upgrade # upgrades every package (kernel too)

Making it cron-ready

We need to make some adjustments to the aptitude command to make it suitable to run in the background:

  • It should not have to wait on user confirmation, because it isn't getting any ;)
  • It should not automatically update kernels (this is still something you should do manually)
  • It should log to a file so you can keep track of it
  • It should not proceed with an upgrade if the update failed

The following command takes on all of these above challenges, in just one line:

(aptitude -y update && aptitude -y upgrade) 2>&1 >> /var/log/auto_update.log

Explained

  • -y answers yes to all questions so that takes care of the user confirmation
  • changing dist-upgrade to upgrade will skip kernel updates
  • 2>&1 >> /var/log/auto_update.log forwards all messages (errors (2), and standard (1)) to a logfile
  • && links two commands together, but will not execute the second if the first one failed.

Combined: an aptitude cronjob

We'll link everything together now. Open your crontab editor:

sudo crontab -e

And to execute our upgrade every night at 1AM type:

0 1 * * * (aptitude -y update && aptitude -y upgrade) 2>&1 >> /var/log/auto_update.log

Save and exit your editor, and you are all set! You could check the logfile: /var/log/auto_update.log every once in a while to see if everything is still running smoothly.

 


Like this article?

   Then Digg it!
Or use another bookmark button below to show your support &
help me spread the word.


tags: ubuntu, crontab, security
category: How to - System
read: 6,010 times

Add comment

» Currently away on vacation. I can reply your message the 24th of July 2008. Please post anyway and check back then. Thank you!

for syntax highlighting

[CODE="Javascript"]
your_code_here();
[/CODE]

Replace "Javascript"
with "php", "text", etc.
code (to make sure you are not a spammer)

Comments

#10. Jonas on 01 July 2008

JonasVery useful article - thanks.

Note that the "upgrade" option has been deprecated in favor of "safe-upgrade" (which aptitude was kind enough to let me know when I ran your command).

#9. Kevin on 17 May 2008

Kevin@ gasull: You have to specify what language. text or bash would do the trick. But your point is clear, I'll update the article! Thanks

#8. gasull on 17 May 2008

gasull[code]sudo crontab -e[/code]

#7. alex on 17 August 2007

alexP.S. I usually include "aptitude clean" as well as all the traffic goes through apt-cacher.

#6. alex on 17 August 2007

alexYou may use "&>" to redirect both stdout and stderr.

#5. Kevin on 08 August 2007

KevinHi Tim, because aptitude has better dependency resolving capabilities.

#4. Tim on 08 August 2007

TimThis maybe a stupid question, but why do you use aptitude and not apt-get?

#3. Ubuwu on 31 July 2007

UbuwuIf you only want security updates, it is easier to just install the unattended-upgrades package.

#2. Kevin on 30 July 2007

KevinHi Ross, no problem. I don't use wordpress, I wrote this blogging tool myself. Cheers!

#1. Ross on 30 July 2007

Rossheya -

Sorry to abuse your comment form like this but I couldn't find an (obvious) 'contact me' link.. For your "Links" section (http://kevin.vanzonneveld.net/links/) do you use a wordpress plugin for that? If so, which one? Cheers!