Memory cache flush cron if free Memory is less than X

vim /root/

freem=$(free -m|grep Mem|awk '{print $4}')
# change 1024 to other value, for e.g you want set limit to 2GB then change it to 2048
if [ "$freem" -lt 1024 ]
echo 1 > /proc/sys/vm/drop_caches
freenew=$(free -m|grep Mem|awk '{print $4}')
echo "Server had only $freem MB free RAM, After memory cache is flushed, free RAM is $freenew MB" | mail -s "Memory cache flushed on server IP_HERE" [email protected]
echo "Nothing to do"

chmod 775
# Now add below line to ‘crontab -e
* * * * * bash /root/

Leave a Reply