Skip to content

Instantly share code, notes, and snippets.

@katopz
Created June 19, 2019 06:03
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 katopz/45cd6ccb59b046c1c71a130cd728c9d7 to your computer and use it in GitHub Desktop.
Save katopz/45cd6ccb59b046c1c71a130cd728c9d7 to your computer and use it in GitHub Desktop.
Set Swap 4GB, Swappiness 10.
# Execute
run() {
# Set Swap 4GB, Swappiness 10.
# ref : https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04
# Turn the swapfile off if has any.
sudo swapoff -a
# Check current disk usage.
df -h
# Create and Enable the Swap File 4GB.
sudo fallocate -l 4G /swapfile
# Ensure amount of space was reserved
ls -lh /swapfile
## Expected
# -rw------- 1 root root 4.0G May 12 13:42 /swapfile
# set up the swap space
sudo mkswap /swapfile
## Expected
# Setting up swapspace version 1, size = 4194300 KiB
# no label, UUID=d005a922-31f2-424a-bfe2-97d2455944e7
# Activating the swap file.
sudo swapon /swapfile
# See swap summary.
sudo swapon -s
## Expected
# Filename Type Size Used Priority
# /swapfile file 4194300 0 -1
# Ensure that the swap is permanent by adding it to the fstab file.
echo '/swapfile none swap sw 0 0' >> /etc/fstab
# Free!
free -m
## Expected
# total used free shared buffers cached
# Mem: 490 183 306 0 21 93
# -/+ buffers/cache: 68 421
# Swap: 4095 0 4095
# Peeking at append result.
cat /etc/fstab
# Swappiness to 10 will cause swap to act as an emergency buffer, preventing out-of-memory crashes.
echo 10 | sudo tee /proc/sys/vm/swappiness
echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf
# Show swappiness.
cat /proc/sys/vm/swappiness
# Correct permissions on the swap file
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile
}
# Process
PROCESS='setup-swapfile'
echo -e "$(tput setaf 3)[$PROCESS] : Begin$(tput sgr0)"
run
echo -e "$(tput setaf 2)[$PROCESS] : End$(tput sgr0)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment