Shell script to create swap file on linux

Shell script to create swap file on linux

A Simple Shell script to create swap on linux. Most of the times on server we need to create swap in case of high memory usage. So here is a script that will create a swap on linux.

Git repo:

You may also like: Test Shellscript on circleci using shellcheck

Download the Script

Firstly we will download the swap script from github. The swap script is available in github. You can add new features and bugs on github.

wget -O

In the above command we used wget to download the script in

Now we need to make this file executable. Use the following command to make it executable.

chmod +x

Create Swap

Now in this step we will create swap. Creating swap is easy.  The script will first check if swap is already there it will not create swap or if swap is not there it will create a file /var/lib/swap. You can change the file also.

sh 2G

The above command will create 2GB swap.

For more usage you can run the following command. It will show all the usage.

Usage: {size}
(Default path: /var/lib/swap)

Available options:

size - Size of swap ( Example - 1G,2G or 1024M)
path - Path to create a swapfile

If you get any issues or bug please feel free to open an issue in github.

Leave a Reply

Your email address will not be published.


The reCAPTCHA verification period has expired. Please reload the page.

48-Hour Flash Sale! Courses from just $10.99