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: https://github.com/chetankapoor/swap.git

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 https://raw.githubusercontent.com/chetankapoor/swap/master/swap.sh -O swap.sh

In the above command we used wget to download the script in swap.sh.

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

chmod +x swap.sh

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 swap.sh 2G

The above command will create 2GB swap.

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

sh swap.sh
Usage: swap.sh {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.