Nginx产生的日志都是存在一个文件,网站运行时间越长,日志文件的大小也在不断增长,这对分析当天日志非常的不方便,所以需要每天把日志文件分割出来,并以时间命名,产生的日志无限增长,终有一天会导致磁盘爆满,而影。
创建日志分割脚本,保存为cut_logs.sh
#!/bin/bash #function:cut nginx log files
#set the path to nginx log files log_files_path="/home/nocome.com/logs/" log_files_dir=${log_files_path} #set nginx log files you want to cut log_files_name=(access ) #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 l o g f i l e s d i r l o g f i l e s n u m = log_files_dir log_files_num= logfilesdirlogfilesnum={#log_files_name[@]}
#cut nginx log files for((i=0;i<$log_files_num;i++));do mv l o g f i l e s p a t h {log_files_path} logfilespath{log_files_name[i]}.log l o g f i l e s d i r {log_files_dir} logfilesdir{log_files_name[i]}.log_$(date -d “yesterday” +"%Y-%m-%d") done
#delete 30 days ago nginx log files find l o g f i l e s p a t h − m t i m e + log_files_path -mtime + logfilespath−mtime+save_days -exec rm -rf {} ;
#restart nginx $nginx_sbin -s reload 复制代码
输入crontab -e命令,添加如下代码,每天0点00分启动。
00 00 * * * /nocome/cut_logs.sh
转自:http://www.nocome.com/thread-32636-1-2.html