blog.zhuohua.store's Archiver

admin 发表于 2019-9-15 21:13

切割Nginx的日志文件

笺注:这是在 [url=http://blog.zhuohua.store/viewthread.php?tid=79&extra=page%3D1]LNMP一键安装包(lnmp_CentOS6.9) [/url]的基础上进行的。


站点的Nginx日志文件:
[root@localhost ~]# cd /home/wwwlogs/
[attach]8852[/attach]


自带的Nginx日志文件切割脚本:(编辑前记得先备份)
[attach]8853[/attach]


[attach]8854[/attach]


[attach]8855[/attach]
注释:这里也要修改一下。





脚本cut_nginx_logs.sh的完整代码:
[root@iZj6c1a39n0ss415rjbuoqZ tools]# cat cut_nginx_logs.sh
#!/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="/home/wwwlogs/"
log_files_dir=${log_files_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")
#set nginx log files you want to cut
[color=Blue]log_files_name=(access blog.zhuohua.store)[/color]
#set the path to nginx.
nginx_sbin="/usr/local/nginx/sbin/nginx"
#Set how long you want to save
[color=Blue]save_days=2[/color]

############################################
#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 {} \;
[color=Blue]find $log_files_path -name "*.log" -a -mtime +$save_days | xargs rm -rf[/color]
$nginx_sbin -s reload
[root@iZj6c1a39n0ss415rjbuoqZ tools]#
[root@iZj6c1a39n0ss415rjbuoqZ tools]#



手动运行脚本:
[root@iZj6c1a39n0ss415rjbuoqZ ~]# [color=Blue]bash /root/lnmp1.3-full/tools/cut_nginx_logs.sh[/color]

[attach]8856[/attach]
注释:
脚本运行后,站点的日志文件会被清空。
另外access.log的大小也变为0了。




######
结合任务计划:
[root@iZj6c1a39n0ss415rjbuoqZ ~]# crontab -e -u root
追加:

1 12 * * * bash /root/lnmp1.3-full/tools/cut_nginx_logs.sh
注释:每天的12点01分自动运行日志切割的脚本


8 2 * * * /sbin/init 6
注释:每天的2点08分自动重启系统


效果:(只保留3天切割后的日志文件)
[attach]8857[/attach]




相关文章:
[url=http://blog.zhuohua.store/viewthread.php?tid=87&page=1&extra=#pid88]本地备份站点目录和数据库+crontab计划任务[/url]





#################################
#################################
[url=https://weidian.com/?userid=823531601&wfr=wx&sfr=app&source=shop]亲,学习研究也要劳逸结合哦,来我微店逛逛,买点东西好好犒劳犒劳自己和家人吧^_^^_^[/url]

[url=https://weidian.com/item.html?itemID=905482571142028053079&wfr=wx&sfr=app&source=goods_home]休闲零食传承世家风干牛肉干 手撕风干牛肉四川特产【非偏远地区满79包邮】[/url]
[url=https://weidian.com/item.html?itemID=905482571142028053079&wfr=wx&sfr=app&source=goods_home][attach]2187[/attach][/url]

[url=https://weidian.com/item.html?itemID=905482571141828030433&wfr=wx&sfr=app&source=goods_home]长虹办公室鼠标加热保暖桌垫毯电脑暖手桌面发热板电热台板写字台[/url]
[url=https://weidian.com/item.html?itemID=905482571141828030433&wfr=wx&sfr=app&source=goods_home][attach]2185[/attach][/url]

[url=https://weidian.com/item.html?itemID=905482571141697401852&wfr=wx&sfr=app&source=goods_home]飞科电熨斗蒸汽家用熨斗家用电烫斗蒸气手持迷你电熨斗FI9308包邮[/url]
[url=https://weidian.com/item.html?itemID=905482571141697401852&wfr=wx&sfr=app&source=goods_home][attach]2189[/attach][/url]

[url=https://weidian.com/item.html?itemID=905482571142562276239&wfr=wx&sfr=app&source=goods_home]新款男长款拉链潮男士钱包男士手拿包 商务皮夹钱夹-JKPJ1806[/url]
[url=https://weidian.com/item.html?itemID=905482571142562276239&wfr=wx&sfr=app&source=goods_home][attach]2186[/attach][/url]

[url=https://weidian.com/item.html?itemID=905482571142001608813&wfr=wx&sfr=app&source=goods_home]韩版定型斜挎单肩手提包SN-两层活动女包8874[/url]
[url=https://weidian.com/item.html?itemID=905482571142001608813&wfr=wx&sfr=app&source=goods_home][attach]2188[/attach][/url]

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.