Skip to content

Instantly share code, notes, and snippets.

@zh4n7wm
Created July 17, 2020 02:02
Show Gist options
  • Select an option

  • Save zh4n7wm/887403378f838cf13e549872d7401309 to your computer and use it in GitHub Desktop.

Select an option

Save zh4n7wm/887403378f838cf13e549872d7401309 to your computer and use it in GitHub Desktop.
add swap file

centos

#!/bin/bash

if [[ $UID -ne 0 ]]; then
    echo "please run this script with root"
    exit
fi

length="8G"  # only support nG
dd_count=$((1024 * $(echo $length | grep -Eo '\d+')))
fallocate -l $length /swapfile
dd if=/dev/zero of=/swapfile bs=1M count=$dd_count
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
if ! grep -q '/swapfile' /etc/fstab; then
    bash -c 'echo "/swapfile swap swap defaults 0 0" >> /etc/fstab'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment