标签:nginx日志
1>vi /usr/local/webserver/nginx/logs/cut_nginx_log.sh
#!/bin/bash #function:cut nginx log files for lnmp #author: http://lnmp.org #set the path to nginx log files log_files_path="/usr/local/webserver/nginx/logs/" log_files_dir="/usr/local/webserver/nginx/logs/old/" #set nginx log files you want to cut log_files_name="access.log" save_days=30 mkdir -p ${log_files_dir} ############################################ #Please do not modify the following script # ############################################ cp ${log_files_path}${log_files_name} ${log_files_path}${log_files_name}_bak #循环生成删除3号到12号的旧日志, #for((i=3;i<13;i++)); #do #if [ $i -lt ‘10‘ ];then #d=0$i #else #d=$i #fi #11/Oct/2015 #date_g=$(date -d last-day +"%d/%b/%Y") #date_g=$d/Oct/2015 #20151011 #date_l=$(date -d last-day +"%Y%m%d") #date_l=201510$d #11\/Oct\/2015 #date=$(date -d last-day +"%d\/%b\/%Y") #date="$d\/Oct\/2015" #cat ${log_files_path}${log_files_name} | grep ${date_g} >${log_files_dir}www_access_${date_l}.log #sed -i /${date}/d ${log_files_path}${log_files_name} #done #11/Oct/2015 昨天日期 date_g=$(date -d last-day +"%d/%b/%Y") #20151011 昨天日期 date_l=$(date -d last-day +"%Y%m%d") #11\/Oct\/2015 昨天日期 date=$(date -d last-day +"%d\/%b\/%Y") cat ${log_files_path}${log_files_name} | grep ${date_g} >${log_files_dir}www_access_${date_l}.log sed -i /${date}/d ${log_files_path}${log_files_name} #delete 30 days ago nginx log files find ${log_files_dir} -mtime +${save_days} -exec rm -rf {} \; #重启nginx服务,否则不知道为什么,日志不再增长 /usr/local/webserver/nginx/sbin/nginx -s reload
2>chmod 777 /usr/local/webserver/nginx/logs/cut_nginx_log.sh #运行的权限
3>crontab -e
尾部追加:
0 4 * * * /usr/local/webserver/nginx/logs/cut_nginx_log.sh
4>crontab -l #查看是否成功
本文出自 “喵呜” 博客,请务必保留此出处http://aijinnini.blog.51cto.com/10783125/1702380
标签:nginx日志
原文地址:http://aijinnini.blog.51cto.com/10783125/1702380