linux 中为了检测linux中存储log不能无限大的情况,使用脚本去控制文件大小的存储以及存储满的时候的处理。
#!/bin/bash
echo "nfs-gw-log_check"
echo "version: 1.0.0"
#Get APP Name
case $1 in
app=$1
;;
*)
echo "Error - log_check! Please indicate APP name firstly."
exit 1
;;
esac
#Get Log Name
log_name=$2
echo "Logname is ${log_name}."
#Define Variables
check_log_time=10
max_log_files=10
max_log_size=1000000
log_dir="/home/nfs-gw/log/${app}"
log_dir_bk="/home/nfs-gw/log/${app}/${log_name}"
#Create Directory
if [ ! -d ${log_dir} ]; then
mkdir -p ${log_dir}
fi
if [ ! -d ${log_dir_bk} ]; then
mkdir -p ${log_dir_bk}
fi
#Initialize Log Number
log_number=0
for filename in `ls $log_dir_bk`
do
if [ $log_number -lt $filename ]
then
log_number=$filename
fi
done
echo "lastest log number is: $log_number"
#Check and Backup
while true
do
sleep ${check_log_time}
s=`stat -c %s "${log_dir_bk}/${log_name}.txt"`
echo "log size: ${s}"
if [ $s -gt $max_log_size ]
then
let log_number+=1
if [ $log_number -gt $max_log_files ]
then
let del_file=log_number-max_log_files
echo "delete file ${del_file}"
rm "${log_dir_bk}/${del_file}"
fi
echo "log size over $max_log_size, save log: ${log_number}"
cp "${log_dir_bk}/${log_name}.txt" "${log_dir_bk}/${log_number}" && cat /dev/null > "${log_dir_bk}/${log_name}.txt"
else
continue
fi
done
```bash
#!/bin/bash
log_path="/home/log"
if [ ! -d "${log_path}/log/log#002" ]; then
mkdir -p "${log_path}/log/log#002"
fi
./log.sh >> /home/nfs-gw/log/NUP/NUP#002/NUP#002.txt 2>&1 &
./log_check NUP NUP#002 > /home/log.txt 2>&1 &