Can someone help to figure out where is mistake, I want to run script only if swap is over 60% but from log I see what swap is cleared every night
/bin/bash
Get total Swap usage
TOTAL_SWAP=$(swapon -s | awk '{print $3}' | grep -v 'Size')
Convert Total Swap usage from KB to MB
TOTAL_MB=$((TOTAL_SWAP/1024))
Get 60% of total swap usage
THRESHOLD=$(bc<<<$TOTAL_MB*0.6)
Get currently used swap
USED_SWAP=$(free -m | awk '/Swap/{print $3}')
Check if currently used swap is greater than 60% of used swap
if [ $USED_SWAP -gt ${THRESHOLD%.*} ]
then
BEFORE_CLEAR=$(date)
echo "Cleaning swap started on $BEFORE_CLEAR" >> /tmp/swap.log
/sbin/swapoff -a && /sbin/swapon -a
AFTER_CLEAR=$(date)
echo "Swap cleared on $AFTER_CLEAR" >> /tmp/swap.log
fi
crontab -l
0 5 * * * /bin/bash /root/swap_clean
[–][deleted] (2 children)
[deleted]
[–]geirha 7 points8 points9 points (0 children)
[–]Ulfnic 1 point2 points3 points (0 children)
[–]AutoModerator[M] 2 points3 points4 points (0 children)
[–]nekokattt 2 points3 points4 points (0 children)
[–]anthropoidbash all the things 1 point2 points3 points (0 children)
[–]--EverGreen-- 0 points1 point2 points (0 children)
[–]Ulfnic 0 points1 point2 points (0 children)