swapon.bash 807 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/bin/bash
  2. ## Ativa swap no servidor
  3. SWAP_FILE_NAME="swapfile.swap"
  4. SWAP_FILE_PATH="/srv/swap/"
  5. USER=`whoami`
  6. SIZE=1048576
  7. if [ ! -z $1 ]
  8. then
  9. let SIZE=SIZE*$1
  10. echo "Will create a file with ${SIZE} *kibibytes* (${1}GB)"
  11. if [ -d $2 ]
  12. then
  13. SWAP_FILE_PATH="${2}"
  14. fi
  15. else
  16. echo "Defaulting to 1GB (${SIZE} kibibytes). Use $0 8 for 8GB (1 gibibyte times 8 in powers of 1024)."
  17. fi
  18. SWAP_FILE="${SWAP_FILE_PATH}/${SWAP_FILE_NAME}"
  19. echo "Will use ${SWAP_FILE} as swap file."
  20. if [ "${USER}" == "root" ]
  21. then
  22. if [ ! -f "${SWAP_FILE}" ]
  23. then
  24. mkdir -p "${SWAP_FILE_PATH}"
  25. dd if=/dev/zero of="${SWAP_FILE}" bs=1024 count=${SIZE} status=progress
  26. fi
  27. chmod -v 600 "${SWAP_FILE}"
  28. mkswap -c "${SWAP_FILE}"
  29. swapon -v "${SWAP_FILE}"
  30. free -ltw
  31. else
  32. echo "You are not root. GTFO."
  33. fi