Automatic Upgrades through Cron

LifeInTheGrey
  12 years ago
  6

Let me start out by saying the MintUpdate application is GREAT ... I love the thing, much better than the standard GNOME (or even KDE) alternative, I'm a big fan. But I know that there is a block of users who don't want to think about upgrades ... they just update and upgrade when necessary, and don't really concern themselves with level 1 vs level 3 upgrades, or apt-pinning, or anything of that sort. These users just want to have an up-to-date system with as little work as possible. For these users (which I encounter frequently with my custom rig business), I have created basic cron scripts that update the system based on appropriate intervals in the background ... no thought or work involved. Time to share!

 

***DISCLAIMER: AS LONG AS THESE SCRIPTS ARE IN PLACE, YOU WILL HAVE ZERO INFLUENCE OVER WHAT IS UPGRADED OR NOT. IF YOU ARE THE TYPE TO ONLY UPGRADE CERTAIN PACKAGES BUT NOT OTHERS, OR WANT MORE CONTROL OVER THE UPDATE/UPGRADE PROCESS, STICK TO MINTUPDATE. IT WORKS WELL.***

 

Pretty much everything here is done in terminal, so unless it is preceeded by *, it is a command done as root. I use XFCE so I use mousepad as a text editor, but if you use GNOME substitute gedit, if you use KDE substitute kate, and if you use LMDX substitute leafpad.

 

1) Install anacron

- aptitude install anacron

     * This will ensure that the cron scripts are run daily during idle time

 

2) Create log files

 

- mousepad /var/log/apt-safe-upgrade

     * save it as a blank file and exit

- mousepad /var/log/apt-full-upgrade

     * save it as a blank file and exit

 

3) Create the upgrade scripts

 

- mousepad /etc/cron.daily/apt-safe-upgrade

     * Paste the following:

 

#!/bin/sh

echo "*********************" >> /var/log/apt-safe-upgrade

date >> /var/log/apt-safe-upgrade

aptitude update >> /var/log/apt-safe-upgrade

aptitude safe-upgrade -o Aptitude::Delete-Unused=false --assume-yes >> /var/log/apt-safe-upgrade

echo "Updates (if any) installed" >> /var/log/apt-safe-upgrade

 

     * Save the file and exit

 

- mousepad /etc/cron.monthly/apt-full-upgrade

     * Paste the following:

 

#!/bin/sh

echo "*********************" >> /var/log/apt-full-upgrade

date >> /var/log/apt-full-upgrade

aptitude update >> /var/log/apt-full-upgrade

aptitude full-upgrade -o Aptitude::Delete-Unused=false --assume-yes >> /var/log/apt-full-upgrade

echo "Updates (if any) installed" >> /var/log/apt-full-upgrade

 

     * Save the file and exit

 

4) Create log rotation files

 

- mousepad /etc/logrotate.d/apt-safe-upgrade

     * Paste the following:

 

/var/log/apt-safe-upgrade {

rotate 2

daily

size 250k

compress

notifempty

}

 

     * Save the file and exit

 

- mousepad /etc/logrotate.d/apt-full-upgrade

     * Paste the following:

 

/var/log/apt-full-upgrade {

rotate 2

monthly

size 250k

compress

notifempty

}

     * Save the file and exit

 

5) Make the scripts executable

 

- chmod +x /etc/cron.daily/apt-safe-upgrade

- chmod +x /etc/cron.monthly/apt-full-upgrade

 

6) Choose settings for configuration files

 

- mousepad /etc/apt/apt.conf.d/local

     * Paste the following:

 

Dpkg::Options {

"--force-confdef";

"--force-confold";

}

 

     * "confdef" sets the behavior to use new version when the file wasn't modified, keep old when it was

     * "confold" makes it force using the old file if it still wants to prompt for some reason

     * if you want to overwrite the current file by default, replace both with just "--force-confnew"

 

Bam, now you're all set up for automatic updates via cron. If you don't believe me, over the next day or two check out /var/log/apt-safe-upgrade and see if it ran!

 

** UPDATE FOR LAPTOPS **

 

So anacron, being a cron job itself, apparently is deferred when a laptop is running on battery! This means that if you only charge your laptop/netbook while off and only have it on while on battery, this job will never run. The solution (not elegant, but it works) is to modify /etc/init.d/anacron ... NOTE: the reason anacron defers running until charging is to save battery life, and by letting it run it will reduce battery life. That said, on my netbook, which after a year is down to 5 hours life, lost about 10 minutes ... totally worth it IMO. But here we go:

 

- mousepad /etc/init.d/anacron

     - modify this: (only partially shown)

          log_progress_msg "deferred while on battery power"

          log_end_msg 0

          exit 0

     - to this:

          log_progress_msg "running while on battery power"

          log_end_msg 0

#        exit 0

 

That's it! Just comment out the exit line and it will continue as normal. You can choose whatever progress message you want as well; personally I have it tell me I look good today. :P Enjoy!

Comments
LifeInTheGrey 12 years ago

Updated the tutorial to include configuration file setup and anacron mod for laptops. Basically, when a new install prompts you during setup, you can set tat behavior in another file. Because anacron defers while on battery by default, if you want it to run the updates while on battery, you need to comment out a line in the anacron init file.


LifeInTheGrey 12 years ago

Updated the tutorial, because I realized I forgot a very important part: anacron. These scripts would run just fine if you left the computer on all the time, but most people (including myself) turn it off from time to time. Anacron gets cron jobs to run during system idle time, so basically its a requirement for these scripts to work properly.


blueXrider 12 years ago

Nice, Thanks.