Created
October 21, 2015 21:18
-
-
Save dhayab/7dcc69b381a2add2b249 to your computer and use it in GitHub Desktop.
Creates a swapfile and sets specific parameters on Ubuntu-based cloud VMs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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