Skip to content

apt-get via cron

IRL I’ve had a few people ask me about updating Ubuntu installations via cron. If you add something like 05 0 * * * /usr/bin/apt-get -y update ; /usr/bin/apt-get -y upgrade to root’s crontab, you’ll get errors like this:

dpkg: warning: 'ldconfig' not found in PATH or not executable.
dpkg: warning: 'start-stop-daemon' not found in PATH or not executable.
dpkg: warning: 'update-rc.d' not found in PATH or not executable.
dpkg: 3 expected program(s) not found in PATH or not executable.
NB: root's PATH should usually contain /usr/local/sbin, /usr/sbin and /sbin.
E: Sub-process /usr/bin/dpkg returned an error code (2)

Those errors should not surprise those who have troubleshooted cron jobs before. Cron 101 teaches us to use always use the full path. If you want to apt-get update via cron, add the following to root’s crontab:

05 0 * * * PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/aleida ; /usr/bin/apt-get -y update ; /usr/bin/apt-get -y upgrade

(You can use output redirection to prevent receiving mail about it but if you’re going to auto-pilot your updates, I recommend having them at least sent to you. You can add a line like MAILTO=cron@example.com to your crontab and direct that email somewhere less noisy than your primary address.)

One Comment

  1. You can use apticron also, if you don’t like the idea of firing off un-regulated updates. It creates a nicely formatted email with all the updates pending for a machine. Of course, you still have to log into each machine individually and apt-get upgrade, but when it comes to servers, new versions of software come with new bugs.

    …of course, if you want the ultimate in control with Ubuntu, just run a Landscape server.

    Posted on 09-Mar-11 at 10:29 am | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*