Skip to content

Instantly share code, notes, and snippets.

@peterchester
Last active April 13, 2024 20:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save peterchester/4537ed05a790045dd11f to your computer and use it in GitHub Desktop.
Save peterchester/4537ed05a790045dd11f to your computer and use it in GitHub Desktop.
A simple little shell script that executes the digital ocean swap file tutorial.
#/bin/sh
# Creates a 1gb swap image.
# @see https://www.digitalocean.com/community/tutorials/how-to-configure-virtual-memory-swap-file-on-a-vps
if [ -f /var/swap.img ]; then
echo "Swap file already exists."
else
touch /var/swap.img
chmod 600 /var/swap.img
dd if=/dev/zero of=/var/swap.img bs=1024k count=1000
mkswap /var/swap.img
swapon /var/swap.img
echo "/var/swap.img none swap sw 0 0" >> /etc/fstab
sysctl -w vm.swappiness=30
free
echo "Swap created and added to /etc/fstab for boot up."
fi
@peterchester
Copy link
Author

Usage:

curl -o swap.sh https://gist.githubusercontent.com/peterchester/4537ed05a790045dd11f/raw/51121ac2d3a370e2c27032c9e14c0ca2bbf2b382/swap.sh
sudo /bin/sh swap.sh
rm -rf swap.sh

@pesach
Copy link

pesach commented Oct 28, 2016

You may want to add this line not to lose swappiness after reboot:
echo "vm.swappiness=30" >> /etc/sysctl.conf

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