Automate Linux Swap File Creation and Removal with Simple Shell Scripts
This guide shows how to check existing swap space, then use two lightweight shell scripts to automatically create, mount, and later remove a swap file on Linux, including code examples, permission steps, and verification commands.
How to Check Current Swap Size
Use the free -h command to view memory usage and swapon --show to list active swap devices. The example output shows a 2 GB swap partition currently in use.
Creating a Swap File
Create a file named create_swap.sh with the following content and give it execute permission.
$ nano create_swap.sh
#!/bin/sh
# size of swapfile in megabytes
swapsize=1024
# does the swap file already exist?
grep -q "swapfile" /etc/fstab
# if not then create it
if [ $? -ne 0 ]; then
echo 'swapfile not found. Adding swapfile.'
fallocate -l ${swapsize}M /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap defaults 0 0' >> /etc/fstab
else
echo 'swapfile found. No changes made.'
fi
echo '--------------------------------------------'
echo 'Check whether the swap space created or not?'
echo '--------------------------------------------'
swapon --showMake the script executable: $ sudo chmod +x create_swap.sh Run it to create and mount the 1 GB swap file:
$ sudo ./create_swap.sh
swapfile not found. Adding swapfile.
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
...
NAME TYPE SIZE USED PRIO
/dev/sda5 partition 2G 954.1M -1
/swapfile file 1024M 0B -2After execution, the new swap file appears in the list. Reboot to ensure it is used.
Removing a Swap File
Create a script named remove_swap.sh with the following content and make it executable.
$ nano remove_swap.sh
#!/bin/sh
# does the swap file exist?
grep -q "swapfile" /etc/fstab
# if it does then remove it
if [ $? -eq 0 ]; then
echo 'swapfile found. Removing swapfile.'
sed -i '/swapfile/d' /etc/fstab
echo "3" > /proc/sys/vm/drop_caches
swapoff -a
rm -f /swapfile
else
echo 'No swapfile found. No changes made.'
fi
echo '--------------------------------------------'
echo 'Check whether the swap space removed or not?'
echo '--------------------------------------------'
swapon --showGive the script execute permission: $ sudo chmod +x remove_swap.sh Run it to remove and unload the swap file:
$ sudo ./remove_swap.sh
swapfile found. Removing swapfile.
swapoff: /dev/sda5: swapoff failed: Cannot allocate memory
--------------------------------------------
Check whether the swap space removed or not?
--------------------------------------------
NAME TYPE SIZE USED PRIO
/dev/sda5 partition 2G 951.8M -1The swap file entry disappears from the output, confirming its removal.
Summary
These two concise shell scripts automate the otherwise manual steps of creating, mounting, and later removing a Linux swap file, allowing you to adjust swap size and filename by editing a few variables.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
