Add swap on VPS

#!/usr/bin/env bash
if [ "$(whoami)" != "root" ]; then
SUDO=sudo
fi
if $(type apt-get > /dev/null 2>&1); then
${SUDO} fallocate -l 2G /swapfile
else
${SUDO} dd if=/dev/zero of=/swapfile count=8 bs=256MiB
fi
${SUDO} chmod 0600 /swapfile
${SUDO} mkswap /swapfile
${SUDO} swapon /swapfile
${SUDO} cp /etc/fstab /etc/fstab.bak
echo "/swapfile none swap sw 0 0" | ${SUDO} tee -a /etc/fstab
echo 10 | ${SUDO} tee /proc/sys/vm/swappiness
echo vm.swappiness = 10 | ${SUDO} tee -a /etc/sysctl.conf
echo fs.inotify.max_user_watches=524288 | ${SUDO} tee -a /etc/sysctl.conf
${SUDO} chown root:root /swapfile
${SUDO} sysctl -p
exit 0

view raw
swap.sh
hosted with ❤ by GitHub