Skip to content

Instantly share code, notes, and snippets.

@evaristorivi
Forked from garystafford/create_swap.sh
Created June 14, 2020 09:44
Show Gist options
  • Save evaristorivi/1c14acc725225db01547667a63077b40 to your computer and use it in GitHub Desktop.
Save evaristorivi/1c14acc725225db01547667a63077b40 to your computer and use it in GitHub Desktop.
From my blog post, Scripting Linux Swap Space: Scripting Linux Swap Space
#!/bin/sh
# size of swapfile in megabytes
swapsize=2024
# 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
sysctl vm.swappiness=10
echo vm.swappiness=10 >> /etc/sysctl.conf
sysctl vm.vfs_cache_pressure=50
echo vm.vfs_cache_pressure=50 >> /etc/sysctl.conf
else
echo 'swapfile found. No changes made.'
fi
# output results to terminal
cat /proc/swaps
cat /proc/meminfo | grep Swap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment