- Swap file on! Only when you need it…
- Create a permanent swap file equal to RAM bytes
Create a permanent swap file equal to RAM bytes.
Yeah I know this is the lazy admin talking, but the inside wants out …
Also, notice that the line 6
sets swappiness to 10
(which is normal for a server)
The script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
if free | awk '/^Swap:/ {exit !$2}'; then echo "Have swap.Exiting" exit 0; else fallocate -l $(( $(cat /proc/meminfo | grep MemTotal | awk '{print $2}') *1024)) /swapfile echo "vm.swappiness=10" >> /etc/sysctl.conf fi chmod 600 /swapfile mkswap /swapfile swapon /swapfile swapon -s echo "/swapfile none swap sw 0 0" >> /etc/fstab |
Deploying directly to your server
Copy this onliner and run it, as root, in your server. Confirm via free -m
1 |
wget https://gist.github.com/specktator/6d2144babc52ab01358707d2515821b8/raw/0e312c51f158a7dd705c86f73fdd370c70c69d87/swapon.sh; bash swapon.sh |