Skip to content

Instantly share code, notes, and snippets.

@karlbaillie
Created November 30, 2017 11:45
Show Gist options
  • Save karlbaillie/da27cd3acd58822252a44d4d46411898 to your computer and use it in GitHub Desktop.
Save karlbaillie/da27cd3acd58822252a44d4d46411898 to your computer and use it in GitHub Desktop.
Delete files if the mount they live on is getting full
#!/bin/bash
# find out how much space is available on the partition /var/log/app resides upon
SPACE_USED=$(df -P /var/log/app | awk '{ if(NR>1)print }' | awk '{ print $5 }' | tr -d '%')
# set the threshold to clear at once breached (percentage)
SPACE_USED_MAX=80
# get the date in a format we can use
DATE=$(date '+%Y%m%d')
# if we're getting low on space where /var/log/app resides
if [ $SPACE_USED -ge $SPACE_USED_MAX ]; then
# retrieve a list of trace directories
for DIR in $(/bin/find /var/log/app/*/trace -mindepth 1 -maxdepth 1 -type d -exec ls -d '{}' \;); do
DIR_BASENAME=$(basename $DIR)
# if they're not from today
if [ $DIR_BASENAME -ne $DATE ]; then
# then delete them
echo "Deleting:" $DIR
rm -fr $DIR
fi
done
fi
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment