#!/bin/bash # Check if script is run as root if [ "$EUID" -ne 0 ]; then echo "Please run as root or with sudo" exit 1 fi # Exit if swapfile exists if [ -f /swapfile ]; then echo "Swapfile already exists. Exiting..." exit 0 fi # Swap Setup sudo fallocate -l 2G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile # Make swap permanent echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab # Adjust swappiness echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf sudo sysctl -p