Jul 072016
 

A simple cron job removing old kernels.

You may find yourself in situation where linux kernels accumulated under/boot are starting to fill up disk space. Especially painful on older systems with small /boot partition. No automated solution out of the box on Debian/Ubuntu. Here is a workaround, removing all but two newest kernels:

crontab -l

10 23 * * 1 /root/bin/kernel-cleanup.sh

 

vim /root/bin/kernel-cleanup.sh

#!/bin/bash
echo "---------- Starting autoremove, we get rid of the old kernels eating /boot space"|logger
OLD=$(ls -tr /boot/vmlinuz-* | head -n -2 | cut -d- -f2- | awk '{print "linux-image-" $0}')
if [ -n "$OLD" ]; then
 apt-get -qy remove --purge $OLD
fi

 

Bonus hint – same thing for Centos machines

# we need yum-utils
yum install yum-utils

# add cronjob
crontab -e
0 2 * * Sat root /usr/bin/package-cleanup -y --oldkernels --count=2 |logger

On Centos you can also use *installonly_limit* in /etc/yum.conf

 

installonly_limit Number of packages listed in installonlypkgs to keep installed at the same time. Setting
to 0 disables this feature. Default is '3'. Note that this functionality used to be in the "installonlyn"
plugin, where this option was altered via tokeep. Note that as of version 3.2.24, yum will now look in the
yumdb for a installonly attribute on installed packages. If that attribute is "keep", then they will never
be removed.