Thursday, February 11, 2021

scripts to alert admin when hard disk is full, and to delete older files

Proposed solution to alert admin when a server hard disk becomes close to full, and for auto deleting old files - 

1. Script to email us when hard disk becomes full -
https://www.google.com/search?q=script+to+email+me+when+hard+disk+crosses+free
gives this script,
https://www.linuxjournal.com/content/tech-tip-send-email-alert-when-your-disk-space-gets-low

by adjusting the THRESHOLD=90 line, we can adjust when the script will email us. 10 GB out of 500 GB hard disk means we need to put THRESHOLD=98

#!/bin/bash
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=98
if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
    mail -s 'Disk Space Alert' mailid@domainname.com << EOF
Your root partition remaining free space is critically low. Used: $CURRENT%
EOF
fi

And a cron to run this daily like 
@daily ~/ourScriptName.sh

2. Script to automatically delete files older than one month - 

https://www.google.com/search?q=script+to+automatically+delete+files+older+than+one+month
gives us this,
https://tecadmin.net/delete-files-older-x-days/
which lists the steps manually. 

If we just write this as a script and put a cron job like the earlier script, this should work. Something like
find /our/path -name "*.zip" -type f -mtime +30 -delete

No comments:

Post a Comment