Login

Navigation

This articles is published 491 days ago and last updated 490 days ago, some information may be out of date.

Using Bash script to create a swap file in Linux

2023-03-20_162256.jpg

If you are running a Linux system with limited memory, you may find that your system becomes slow or unresponsive when you have multiple applications running simultaneously.

One way to alleviate this issue is to create a swap file, which acts as a virtual memory extension of your RAM.


Full content of script

Copy and paste the following code into the terminal to create a Bash shell script:

#!/bin/bash

# Get total available memory in bytes
total_memory=$(grep MemTotal /proc/meminfo | awk '{print $2 * 1024}')

# Function to create and enable swap file
create_swap() {
  local size=$1
  local swapfile="/swapfile"

  # Create swap file with the specified size
  sudo fallocate -l "$size" "$swapfile"

  # Set permissions
  sudo chmod 600 "$swapfile"

  # Make it a swap file
  sudo mkswap "$swapfile"

  # Enable swap
  sudo swapon "$swapfile"

  # Add swap file to /etc/fstab for auto-mount
  echo "$swapfile none swap sw 0 0" | sudo tee -a /etc/fstab

  echo "Swap file created and enabled successfully."
}

echo "Choose an option:"
echo "1. Double the system memory"
echo "2. Set custom size (in GB)"
echo "3. Exit"

read -p "Enter your choice (1/2/3): " choice

case $choice in
  1)
    # Double the system memory
    swap_size=$((total_memory * 2))
    ;;
  2)
    read -p "Enter custom size in **GB**: " custom_size
    # Convert to bytes
    swap_size=$((custom_size * 1024 * 1024 * 1024))
    ;;
  3)
    echo "Exiting..."
    exit 0
    ;;
  *)
    echo "Invalid option, exiting..."
    exit 1
    ;;
esac

create_swap "$swap_size"

Breakdown the script

In this post, we will go through the steps to create a swap file in Linux using a Bash shell script.

This script creates and enables a swap file on a Linux system. Here's a breakdown of the script:

  • The first line specifies the interpreter to use (/bin/bash).
  • The script uses the grep and awk commands to get the total amount of available memory in bytes from the /proc/meminfo file and stores the value in the total_memory variable.
  • The create_swap function is defined, which takes in the size of the swap file as an argument.
  • Inside the create_swap function, the script creates a swap file of the specified size using the fallocate command.
  • The script then sets the correct permissions on the swap file using the chmod command.
  • Next, the script formats the swap file using the mkswap command.
  • The script then enables the swap file using the swapon command.
  • Finally, the script adds the swap file to /etc/fstab so that it is automatically mounted on boot.
  • The script prompts the user to choose between three options: double the system memory, specify a custom size in GB, or exit the script.
  • Depending on the user's choice, the script calculates the size of the swap file and passes it as an argument to the create_swap function.
  • The script uses a case statement to handle the user's choice. If the user chooses option 1, the script doubles the system memory to determine the size of the swap file. If the user chooses option 2, the script prompts them to enter a custom size in GB and converts it to bytes. If the user chooses option 3, the script exits. If the user chooses an invalid option, the script outputs an error message and exits.
  • Finally, the script calls the create_swap function with the calculated size of the swap file as an argument and outputs a message indicating that the swap file was created and enabled successfully.

Executable the script

Make the script executable by running the following command in the terminal:

chmod +x swap.sh
Replace swap.sh with the name of the file you saved the script in.

Run the script

Run the script by entering the following command in the terminal:

./swap.sh

Congratulations:

Created a swap file in Linux. With a swap file, your system will be able to handle more applications and tasks without slowing down or becoming unresponsive.

Related: