批量检测并关闭机器swap分区脚本
前言:构造模拟环境
dd if=/dev/zero of=/swapfile1 bs=1M count=1024
mkswap /swapfile1
swapon /swapfile1
/etc/fstab加入:
/swapfile1 swap swap defaults 0 0
一、
-
原始状态,没有/swapfile1情形
1、
2、执行脚本输出
二、有/swapfile1,但空闲内存达不到要求的情形,(这里通过改变脚本限制条件,提高为550000M,实现)
dd if=/dev/zero of=/swapfile1 bs=1M count=1024
mkswap /swapfile1
swapon /swapfile1
/etc/fstab加入:
/swapfile1 swap swap defaults 0 0
执行脚本:
三、符合的条件下,清除/swapfile1 交换分区
代码:
#!/bin/bash
if [ ! -f "/swapfile1" ];then
echo "0|success,not has swap file "
exit 0
fi
mem_free=`free -m | grep Mem | awk {print $4}`
swap_used=`free -m | grep Swap | awk {print $3}`
RET=0
if [ ${mem_free} -gt 4000 ] && [ $[swap_uesd] -lt 200 ];then
cp /etc/fstab /tmp/fstab
sed -i /s+swaps+/d /etc/fstab
swapoff -a
if [ $? ];then
ls /swapfile1 && rm -f /swapfile1
if [ $? ];then
RET=0
else
RET=2
fi
else
if [ -f "/swapfile1" ];then
RET=1
fi
fi
else
RET=3
fi
case $RET in
"0")
echo "0|swapoff swap file succeed"
;;
"1")
echo "1|time out,exec swapoff -a failed"
;;
"2")
echo "1|time out,rm -f /swapfile1 failed"
;;
"3")
echo "1|no need to swapoff file ,Manual operation is required"
;;
esac
exit $RET
