Skip to content

Instantly share code, notes, and snippets.

@jinjie
Forked from Repox/swap.sh
Last active May 17, 2020 06:52
Show Gist options
  • Save jinjie/e670c9521b869f1239edde52370168b1 to your computer and use it in GitHub Desktop.
Save jinjie/e670c9521b869f1239edde52370168b1 to your computer and use it in GitHub Desktop.
Shell script for adding swap to Linux
#!/bin/sh
# Usage: curl <url to the raw script> | sh -s <size in MB>
# size of swapfile in megabytes
swapsize=1024
if [ $1 ];
then
swapsize=$1
fi
# 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
dd if=/dev/zero of=/swapfile count=${swapsize} bs=1MiB
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
cat /proc/swaps
cat /proc/meminfo | grep Swap
free -m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment