Last active
October 6, 2022 06:43
-
-
Save kahidna/141425fe3331593354928569cb8e62ff to your computer and use it in GitHub Desktop.
simple script to create a swap file and enable it to system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# scipt for generate and enable swap on linux | |
# tested on ubuntu server 14.04.5 LTS | |
# where you will put swapfile, default is on / | |
BASEFOLDER=/ | |
SWAPFILENAME=swapfile.img | |
# in megabytes | |
SWAPSIZE=4096 | |
echo "" | |
echo "create directory swap" | |
sudo mkdir -vp $BASEFOLDER | |
echo "" | |
echo "create empty file for swap" | |
sudo dd if=/dev/zero of=$BASEFOLDER/$SWAPFILENAME bs=$SWAPSIZE count=1M | |
echo "" | |
echo "setup permission swap to 600" | |
sudo chmod 600 $BASEFOLDER/$SWAPFILENAME | |
echo "" | |
echo "setup empty file to swap " | |
sudo mkswap $BASEFOLDER/$SWAPFILENAME | |
echo "" | |
echo "activate swap" | |
sudo swapon $BASEFOLDER/$SWAPFILENAME | |
echo "" | |
echo "add swap file to /etc/fstab" | |
if [[ "$BASEFOLDER" = "/" ]]; then | |
#statements | |
echo "/$SWAPFILENAME swap swap defaults 0 0" >> /etc/fstab | |
else | |
echo "$BASEFOLDER/$SWAPFILENAME swap swap defaults 0 0" >> /etc/fstab | |
fi | |
echo "" | |
echo "end of script" | |
echo "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment