memory.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/oneinstack/oneinstack
  10. export LANG=en_US.UTF-8
  11. export LANGUAGE=en_US:en
  12. Mem=`free -m | awk '/Mem:/{print $2}'`
  13. Swap=`free -m | awk '/Swap:/{print $2}'`
  14. if [ $Mem -le 640 ]; then
  15. Mem_level=512M
  16. Memory_limit=64
  17. THREAD=1
  18. elif [ $Mem -gt 640 -a $Mem -le 1280 ]; then
  19. Mem_level=1G
  20. Memory_limit=128
  21. elif [ $Mem -gt 1280 -a $Mem -le 2500 ]; then
  22. Mem_level=2G
  23. Memory_limit=192
  24. elif [ $Mem -gt 2500 -a $Mem -le 3500 ]; then
  25. Mem_level=3G
  26. Memory_limit=256
  27. elif [ $Mem -gt 3500 -a $Mem -le 4500 ]; then
  28. Mem_level=4G
  29. Memory_limit=320
  30. elif [ $Mem -gt 4500 -a $Mem -le 8000 ]; then
  31. Mem_level=6G
  32. Memory_limit=384
  33. elif [ $Mem -gt 8000 ]; then
  34. Mem_level=8G
  35. Memory_limit=448
  36. fi
  37. # add swapfile
  38. if [ ! -e ~/.oneinstack ] && [ "${Swap}" == '0' ] && [ ${Mem} -le 2048 ]; then
  39. echo "${CWARNING}Add Swap file, It may take a few minutes... ${CEND}"
  40. dd if=/dev/zero of=/swapfile count=2048 bs=1M
  41. mkswap /swapfile
  42. swapon /swapfile
  43. chmod 600 /swapfile
  44. [ -z "`grep swapfile /etc/fstab`" ] && echo '/swapfile swap swap defaults 0 0' >> /etc/fstab
  45. fi