Skip to content

Instantly share code, notes, and snippets.

@Repox
Created April 27, 2016 06:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save Repox/fab1cbe21d2a20b628d2caee27ea383c to your computer and use it in GitHub Desktop.
Save Repox/fab1cbe21d2a20b628d2caee27ea383c to your computer and use it in GitHub Desktop.
Shell script for adding swap to Linux
#!/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
# output results to terminal
df -h
cat /proc/swaps
cat /proc/meminfo | grep Swap
Copy link

ghost commented Aug 11, 2020

You should use dd, not fallocate! This will cause the swapfile to have holes, making it slower.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment