Skip to content

Instantly share code, notes, and snippets.

@mehargags
Created February 18, 2025 12:21
Show Gist options
  • Save mehargags/3b9cf31a2a5c3b36263c52897cee094d to your computer and use it in GitHub Desktop.
Save mehargags/3b9cf31a2a5c3b36263c52897cee094d to your computer and use it in GitHub Desktop.
Swap-Check-Make.sh
#!/bin/bash
# Check if swap is enabled
if swapon --show | grep -q "swap"; then
echo "Swap is already enabled."
else
echo "Swap is not enabled. Creating a swap file..."
# Create a 2GB swap file using fallocate
fallocate -l 2G /swapfile
# Set the correct permissions
chmod 600 /swapfile
# Set up the swap area
mkswap /swapfile
# Enable the swap
swapon /swapfile
# Verify swap is active
swapon -s
# Backup current fstab before modification
cp /etc/fstab /root/fstab_BAK
# Add the swap entry to /etc/fstab for persistence across reboots
echo "/swapfile none swap sw 0 0" >> /etc/fstab
# Clear the screen and display updated fstab
clear
cat /etc/fstab
echo "Swap file created and enabled. Entry added to /etc/fstab."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment