Skip to content

Instantly share code, notes, and snippets.

@dhayab
Created October 21, 2015 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhayab/7dcc69b381a2add2b249 to your computer and use it in GitHub Desktop.
Save dhayab/7dcc69b381a2add2b249 to your computer and use it in GitHub Desktop.
Creates a swapfile and sets specific parameters on Ubuntu-based cloud VMs.
#!/bin/bash
#
# Info: Creates a swapfile and sets specific parameters on Ubuntu-based cloud VMs.
# Tested with Ubuntu 14.04 on DigitalOcean.
# Based on: https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04
SIZE=1G
FILE=/swapfile
if [ "$EUID" -ne 0 ]
then
echo -e "\033[31mPlease run this script as root.\033[m"
exit 1
fi
if grep -q "${FILE}" /etc/fstab
then
echo -e "\033[31m${FILE} is already present in /etc/fstab. Exiting...\033[m"
exit 1
fi
fallocate -l ${SIZE} ${FILE}
chmod 600 ${FILE}
mkswap ${FILE}
swapon ${FILE}
sysctl vm.swappiness=10
sysctl vm.vfs_cache_pressure=50
echo -e "vm.swappiness=10\nvm.vfs_cache_pressure=50" >> /etc/sysctl.conf
echo -e "${FILE}\tnone\tswap\tsw\t0\t0" >> /etc/fstab
echo "DONE."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment