問題:nginx會(huì)憑據(jù)nginx.conf的設(shè)置生成access.log和error.log,跟著會(huì)見量的增長,日志文件會(huì)越來越大,既會(huì)影響會(huì)見的速度(寫入日志時(shí)間耽誤),也會(huì)增加查找日志的難度,nginx沒有這種按天或更細(xì)粒度生成日志的機(jī)制,所以需要本身添加按時(shí)任務(wù),英國服務(wù)器 俄羅斯主機(jī),支解日志文件。
1.新建支解日志文件的劇本,譬喻存放路徑:/usr/local/nginx/sbin/cut_nginx_logs.sh,按天支解詳細(xì)內(nèi)容:
#!/bin/bash
#function:cut nginx log files for lnmp v0.5 and v0.6
#author: http://lnmp.org
#set the path to nginx log files
log_files_path="/data/nginxlog/"
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 $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]}.log_$(date -d "yesterday" +"%Y-%m-%d")
done
#delete 30 days ago nginx log files
find $log_files_path -mtime +$save_days -exec rm -rf {} \;
$nginx_sbin -s reload
2.將劇本添加到按時(shí)任務(wù)中,我指定天天0點(diǎn)執(zhí)行
//打開按時(shí)任務(wù)
crontab -e
//進(jìn)入編輯模式
i
//添加按時(shí)任務(wù)
00 00 * * * /bin/sh /usr/local/nginx/sbin/cut_nginx_logs.sh
//生存退出
:wq
//查察按時(shí)任務(wù),就會(huì)看到你添加的內(nèi)容了
crontab -l
3.查察實(shí)際結(jié)果