Bootstrap

释放已删除空间资源

查看已删除文件

当遇到df 和du 看到的磁盘空间不一致的时候,大概率是因为文件句柄未释放的情况下,文件被删除;我们可以通过下面命令来查看:

lsof |grep deleted

这些就是已删除文件 

服务可停的情况

如果服务可以停止,可以用下面命令查看占用已删除的线程,并将其kill来释放磁盘资源

lsof |grep deleted   | awk '{print $2}' | uniq | xargs kill -9

如果线上服务,且服务不可停止,则采用下面方法来实现
 

清理已删除文件

Centos清理

#!/bin/sh
if ! [ -x "$(command -v lsof)" ]; then
  echo 'lsof is not installed begin to install' >&2
  yum install -y install lsof
fi
fdnum=$(lsof |grep delete|grep export|awk 'NR==1{print $2}')
if [ ! $fdnum ];then
   echo 'lsof |grep delete|grep export|awk ...is null' >&2
return ;
fi

cd /proc/$fdnum/fd
filehd=$(ls -l|grep export|grep deleted|awk '{print $9}')
ARR=$filehd
echo "clean....";
echo $ARR
for element in $ARR;do
    echo $element;
        echo "0" > $element
done

Ubuntu清理

#!/bin/sh
if ! [ -x "$(command -v lsof)" ]; then
  echo 'lsof is not installed begin to install' >&2
  yum install -y install lsof
fi
fdnum=$(lsof |grep delete|grep export|awk 'NR==1{print $2}')
if [ ! $fdnum ];then
   echo 'lsof |grep delete|grep export|awk ...is null' >&2
return ;
fi

cd /proc/$fdnum/fd
filehd=$(ls -l|grep export|grep deleted|awk '{print $9}')
ARR=$filehd
echo "clean....";
echo $ARR
for element in $ARR;do
    echo $element;
        echo "0" > $element
done;

;