Swap is used for cases, when system run out of available RAM. If server have no available RAM for process, it will crush/hung. Swap way extremely low, comparable to RAM and add extra load to disks.
When you run a lot of Linux servers it is a great idea to have “swap cleaner” clear up swap space during low-load time (night-time usually). Thi script used for this procedure.
Drop it at /etc/cron.daily to make it run daily.
It will check it server have enough available RAM, before running.
Code:
#!/bin/bash
free_mem=”$(free | grep ‘Mem:’ | awk ‘{print $7}’)”
used_swap=”$(free | grep ‘Swap:’ | awk ‘{print $3}’)”
echo -e “Free memory:\t$free_mem kB ($((free_mem / 1024)) MiB)\nUsed swap:\t$used_swap kB ($((used_swap / 1024)) MiB)”
if [[ $used_swap -eq 0 ]]; then
echo “Congratulations! No swap is in use.”
elif [[ $used_swap -lt $free_mem ]]; then
echo “Freeing swap…”
sudo swapoff -a
sudo swapon -a
else
echo “Not enough free memory. Exiting.”
exit 1
fi