Nginx日志切割脚本增强版

2011年5月20日 | 分类: Linux | 标签: , , , , , ,

特点:验证输入的网站日志目录是否存在,验证输入的网站日志文件是否存在,自动添加计划任务切割指定网站的日志。根据VPSer:Nginx日志切割脚本http://www.vpser.net/manage/cut-nginx-log-files.html修改而成。

wget http://www.yishanhome.com/wp-content/uploads/2011/05/set_cut_nginx_logs.sh;sh set_cut_nginx_logs.sh

#!/bin/bash
clear
printf "
#This is a Shell-Based tool
# Author: yishan
# Website: http://www.yishanhome.com
"
# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install lnmp"
    exit 1
fi
cd $HOME
#set the path to nginx log files
log_files_path()
{
log_files_path="/home/wwwlogs/"
 echo "Please input the www log files path:"
  read -p "(default directory: $log_files_path):" log_files_path
  if [ "$log_files_path" = "" ]; then
    log_files_path="/home/wwwlogs/"
  fi
  if [ ! -e $log_files_path ] ; then
echo -e '\033[0;31;1mError wwwlog directory\033[0m'
  log_files_path
  fi
  }
log_files_path
log_files_name()
{
  read -p "Please input the www log files name:" log_files_name
  if [ "$log_files_name" = "" ]; then
    log_files_name
  fi
  if [ ! -e $log_files_path\/$log_files_name.log ] ; then
echo -e '\033[0;31;1mError wwwlog name\033[0m'
  log_files_name
  fi
  }
log_files_name
 
cat >> log_$log_files_name.sh << END
log_files_path=$log_files_path
log_files_dir=\${log_files_path}\$(date -d "yesterday" +"%Y")/\$(date -d "yesterday" +"%m")
 
#set nginx log files you want to cut
log_files_name=$log_files_name
#set the path to nginx.
nginx_sbin="/usr/local/nginx/sbin/nginx"
#Set how long you want to save
save_days=30
 
############################################
#Please do not modify the following script #
############################################
mkdir -p \$log_files_dir
 
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_dir}/\${log_files_name[i]}_\$(date -d "yesterday" +"%Y%m%d").log
done
 
#delete 30 days ago nginx log files
find \$log_files_path -mtime +\$save_days -exec rm -rf {} \; 
 
\$nginx_sbin -s reload
END
echo "00 00 * * * /bin/bash $HOME/log_$log_files_name.sh" >> /etc/crontab
目前还没有任何评论.

返回顶部