From c131393f3e03d0e27c57fc1d0e1dea806c66915e Mon Sep 17 00:00:00 2001 From: xiaoz Date: Tue, 22 Dec 2020 16:07:28 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E8=99=9A=E6=8B=9F=E5=86=85?= =?UTF-8?q?=E5=AD=98=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- set_swap.sh | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ synctime.sh | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 set_swap.sh diff --git a/set_swap.sh b/set_swap.sh new file mode 100644 index 0000000..09da289 --- /dev/null +++ b/set_swap.sh @@ -0,0 +1,51 @@ +#!/bin/bash +##### 设置虚拟内存 ##### +##### update:2020/12/22 ##### +##### author:xiaoz + +#获取物理内存 +p_mem=`free -m|grep Mem|awk '{print $2}'` +#获取最佳虚拟内存大小 + +#内存如果小于2g,则设置2倍 +if [ $p_mem -lt 2000 ] + then + swap_value=$p_mem*2 +elif [ $p_mem -gt 2000 ] && [ $p_mem -lt 8000 ] + #内存大于2G,小于8G,则设置和物理内存大小一致 + then + swap_value=$p_mem +else + #其他情况,比如大于8G,则设置8G内存 + swap_value=8000 +fi + + +#设置swap函数 +function set_swap(){ + #创建swap + dd if=/dev/zero of=/dev/swap bs=1M count=${swap_value} + mkswap /dev/swap + swapon /dev/swap + #写入分区表 + echo '/dev/swap swap swap defaults 0 0' >> /etc/fstab +} + +#判断当前是否开启了虚拟内存 +if [ swapon -s ] +then + echo 'You have already set up swap, there is no need to repeat the settings.' + exit +else + #设置虚拟内存 + set_swap + echo '---------------------------------' + echo 'Swap setup complete.' + echo '---------------------------------' + swapon -s + echo '---------------------------------' + free -mt + echo '---------------------------------' +fi + +exit \ No newline at end of file diff --git a/synctime.sh b/synctime.sh index 8662c5b..bcf44ea 100644 --- a/synctime.sh +++ b/synctime.sh @@ -17,4 +17,4 @@ ntpd=(`which ntpdate`) echo "*/20 * * * * ${ntpd} pool.ntp.org > /dev/null 2>&1" >> /var/spool/cron/root systemctl reload crond -echo "同步成功,当前时间:" date \ No newline at end of file +echo "同步成功,当前时间:" date \ No newline at end of file