Skip to content

Instantly share code, notes, and snippets.

@rickyhewitt
Last active September 27, 2019 10:54
Show Gist options
  • Save rickyhewitt/0e98bcad02fc44df17be to your computer and use it in GitHub Desktop.
Save rickyhewitt/0e98bcad02fc44df17be to your computer and use it in GitHub Desktop.
Quick and easy script to setup swap
#!/bin/sh
# createswap.sh
# creates swap file.
# Optionally specify size (e.g. 1024M)
# If no size is specified, default to physical memory / 4
#
# <ricky@rickyhewitt.me>
if [ $1 ]; then
SWAP_SIZE=$1
else
PHYSICAL_MEM=$(head /proc/meminfo | awk 'BEGIN {OFS = FS} NR==1{print $2}')
SWAP_SIZE=$(($PHYSICAL_MEM/4))
fi
echo "Creating swap file with size of $SWAP_SIZE"
fallocate -l $SWAP_SIZE /swap
mkswap /swap
chmod 600 /swap
swapon /swap
sed -i -e '$a/swap none swap sw 0 0' /etc/fstab
sysctl vm.swappiness=15
sed -i -e '$avm.swappiness=15' /etc/sysctl.conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment