From 04c3331953840e784a1ea829df41b24286e9d4f0 Mon Sep 17 00:00:00 2001 From: anadahz Date: Thu, 1 Sep 2016 17:19:30 -0300 Subject: [PATCH 1/2] Add a daily cronjob emergency deletion of logs/files This script checks the root filesystem disk usage and delete log files and a number of files upon a critical or critical disk space usage of the root filesystem. --- .../etc/cron.daily/emergency_cleanup | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lepidopter-fh/etc/cron.daily/emergency_cleanup diff --git a/lepidopter-fh/etc/cron.daily/emergency_cleanup b/lepidopter-fh/etc/cron.daily/emergency_cleanup new file mode 100644 index 0000000..aa6eb9e --- /dev/null +++ b/lepidopter-fh/etc/cron.daily/emergency_cleanup @@ -0,0 +1,23 @@ +#!/bin/bash +# This script will cleanup old log files and apt cache if a specific warning or +# critical percentage of used disk space is triggered for the root filesystem +set -e + +# Critical percentage of root filesystem usage +CRITICAL_PCENT=98 +# Warning percentage of root filesystem usage +WARNING_PCENT=93 +ROOTFS_USE=$(df --sync --output=pcent / |tail -n1 |cut -d'%') + +if [ "$ROOTFS_USE" -ge "$CRITICAL_PCENT" ] ; then + apt-get clean + echo "Removing documentation..." >&2 + find /usr/share/doc -depth -type f ! -name copyright|xargs rm || true + find /usr/share/doc -empty|xargs rmdir || true + rm -rf /usr/share/man /usr/share/groff /usr/share/info /usr/share/lintian \ + /usr/share/linda /var/cache/man /usr/share/locale + find /var/log/ -type f -mtime +1 -exec rm -f -- '{}' \; +elif [ "$ROOTFS_USE" -ge "$WARNING_PCENT" ] ; then + # Remove the same set of files and directories + find /var/log/ -type f -mtime +2 -exec rm -f -- '{}' \; +fi From e54403da0af4c321c25a1d55a6dc307f6a4dab5f Mon Sep 17 00:00:00 2001 From: anadahz Date: Thu, 1 Sep 2016 17:44:41 -0300 Subject: [PATCH 2/2] Fix remove whitespace from variable, set execute permissions --- lepidopter-fh/etc/cron.daily/emergency_cleanup | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 lepidopter-fh/etc/cron.daily/emergency_cleanup diff --git a/lepidopter-fh/etc/cron.daily/emergency_cleanup b/lepidopter-fh/etc/cron.daily/emergency_cleanup old mode 100644 new mode 100755 index aa6eb9e..c99b5cb --- a/lepidopter-fh/etc/cron.daily/emergency_cleanup +++ b/lepidopter-fh/etc/cron.daily/emergency_cleanup @@ -7,7 +7,7 @@ set -e CRITICAL_PCENT=98 # Warning percentage of root filesystem usage WARNING_PCENT=93 -ROOTFS_USE=$(df --sync --output=pcent / |tail -n1 |cut -d'%') +ROOTFS_USE=$(df --sync --output=pcent / |tail -n1 |cut -d'%' -f1 |tr -d ' ') if [ "$ROOTFS_USE" -ge "$CRITICAL_PCENT" ] ; then apt-get clean