魏长东

weichangdong

定期清理nginx日志的脚本

之前的老项目,把nginx日志存储到了代码目录下,运维就懒得管了。存储的access文件还是一个文件,没有做什么按天或者按小时存储。好在access日志没啥用,就写了个定时清理的工具。

#!/bin/bash
#edit by wcd 20150720
log_files_path1="/data/sites/*"
#set nginx log files you want to cut,but not include '.log'
log_files_name1=(Access Error)

log_files_num1=${#log_files_name1[@]}
#cut nginx log files
for((i=0;i<$log_files_num1;i++));do
mv ${log_files_path1}${log_files_name1[i]}.log ${log_files_path1}${log_files_name1[i]}_bak.log
#delete  nginx log files
rm -f ${log_files_path1}${log_files_name1[i]}_bak.log
done

#########################第二个网站清理############################
log_files_path="/data/sites/*"
#set nginx log files you want to cut,but not include '.log'
log_files_name=(access error)

log_files_num=${#log_files_name[@]}
#cut nginx log files
for((i=0;i<$log_files_num;i++));do
mv ${log_files_path}${log_files_name[i]}.log ${log_files_path}${log_files_name[i]}_bak.log
#delete  nginx log files
rm -f ${log_files_path}${log_files_name[i]}_bak.log
done


#set the path to nginx.
nginx_sbin="/usr/local/nginx/sbin/nginx"
$nginx_sbin -s reload
sleep 2
for((i=0;i<$log_files_num1;i++));do
 chmod  777 ${log_files_path1}${log_files_name1[i]}.log >/dev/null
done

for((i=0;i<$log_files_num;i++));do
 chmod  777 ${log_files_path}${log_files_name[i]}.log >/dev/null
done
dd=`date "+%Y-%m-%d"`
echo "$dd Done";