How to identify percentage of memory usage to perform cache cleanup command?

0

We know that the memory cache cleanup command is this:

sync; echo 3 > /proc/sys/vm/drop_caches

But how to make an executable with an if () else () condition, so that this command only runs when memory usage reaches 80% or more through crontab?

Something like this:

#!/bin/sh
if (uso da memoria > 80%){
    sync; echo 3 > /proc/sys/vm/drop_caches
}
    
asked by anonymous 15.10.2014 / 01:27

1 answer

2

I solved my problem with the code below:

#!/bin/bash

# total de memória instalada  32991100 (32 GB)

# total em 90% de uso 29691990

MAXIMO="29691990"

MONITOR=$(free | grep Mem)
USADA=$(echo $MONITOR | awk '{ print $3 }')
LIVRE=$(echo $MONITOR | awk '{ print $4 }')

if [ "$USADA" -gt "$MAXIMO" ]
then
    sync; echo 3 > /proc/sys/vm/drop_caches
fi

The cron task will run every 5 minutes, and if it identifies that the memory usage is above 90%, it will clear the memory cache.

    
15.10.2014 / 03:31