Skip to content

Instantly share code, notes, and snippets.

@MohamedElashri
Last active September 27, 2022 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MohamedElashri/559c35548e5985ad6e96c461156eec07 to your computer and use it in GitHub Desktop.
Save MohamedElashri/559c35548e5985ad6e96c461156eec07 to your computer and use it in GitHub Desktop.
Bash script to create a swap file on Linux (debian/ubuntu)
#!/bin/bash
# Bash script to create a swap file on Linux (debian/ubuntu)
# Author: Mohamed Elashri
# Date: 2022-09-27
# use free -m to check the memory and see if there is enough memory (less than 2G) to create swap
# Logic is simple
# if there is no enough memory, then create swap
# if there is enough memory, delete swap
if [ $mem -lt 2048 ]
then
if [ $swap -eq 0 ]
then
echo "Creating swap file of 4G"
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
else
echo "There is already a swap file, do you want to delete it and create a new one of 4G? (y/n)"
read ans
if [ $ans == "y" ]
then
echo "Deleting the old swap file"
sudo swapoff -a
sudo rm /swapfile
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
else
echo "Ok, no swap file created"
fi
fi
else
echo "There is enough memory, no need to create swap"
fi
# check the memory again
mem=`free -m | grep Mem | awk '{print $2}'`
swap=`free -m | grep Swap | awk '{print $2}'`
echo "Memory: $mem MB"
echo "Swap: $swap MB"
#!/bin/bash
# Bash script to create a swap file on Linux (debian/ubuntu)
# Author: Mohamed Elashri
# Date: 2022-09-27
# use free -m to check the memory and see if there is enough memory (less than 2G) to create swap
# Logic is simple
# if there is no enough memory, then create swap
# if there is enough memory, delete swap
# check the memory
mem=`free -m | grep Mem | awk '{print $2}'`
swap=`free -m | grep Swap | awk '{print $2}'`
# if there is no enough memory (less than 2G), then create swap memory of 4G
if [ $mem -lt 2048 ]; then
if [ $swap -eq 0 ]; then
dd if=/dev/zero of=/swapfile bs=1024 count=4194304
# Give the correct permission (secure)
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
# Tune linux krenel swappiness parameter
sysctl vm.swappiness=80
echo "vm.swappiness = 80" >> /etc/sysctl.conf
# set cache pressure
sysctl vm.vfs_cache_pressure=50
echo "vm.vfs_cache_pressure = 50" >> /etc/sysctl.conf
fi
fi
# if there is enough memory, make user choose to delete swap or not
if [ $mem -ge 2048 ]; then
if [ $swap -gt 0 ]; then
echo "There is enough memory, and swap memory exists do you want to delete swap?"
echo "1) Y"
echo "2) N"
read -p "Please choose:" choice
case $choice in
1)
swapoff /swapfile
rm -rf /swapfile
sed -i '/swap/d' /etc/fstab
;;
2)
exit 0
;;
esac
fi
fi
echo "Done!"

Usage:

For create_swap.sh script

  • run the script
chmod +x create_swap.sh
 ./create_swap.sh

if everything is ok, we should see something like this

 Memory: 2048 MB
 Swap: 4096 MB

if there is no swap file, we should see something like this

 Memory: 2048 MB
 Swap: 0 MB

if there is already a swap file, we should see something like this

 Memory: 2048 MB
 Swap: 4096 MB

If we want to do this manually without the scrip

  • check the swap file sudo swapon --show

  • delete the swap file

sudo swapoff -a
sudo rm /swapfile
sudo sed -i '/swapfile/d' /etc/fstab

Then check the memory again free -m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment