VPS侦探论坛

 找回密码
 注册
查看: 3335|回复: 0

修改了一下nginx日志切割的脚本,请大家批判一下

[复制链接]
发表于 2017-1-4 18:28:23 | 显示全部楼层 |阅读模式

写每个日志文件,除了error日志,其他的都自动切割
不按照年月分目录,放在一个目录,这样好处理点。
  1. cat /root/bin/cut_nginx_logs.sh
  2. #!/bin/bash
  3. #function:cut nginx log files for lnmp
  4. #author: http://lnmp.org
  5. #modified by http://www.juyimeng.com/lnmp-nginx-log-cut-per-day-rotation.html

  6. #set the path to nginx log files
  7. log_files_path="/home/wwwlogs/"
  8. #log_files_dir=${log_files_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")
  9. log_files_dir=${log_files_path}bak/
  10. #set nginx log files you want to cut
  11. #get log files list,exclude error.log
  12. log_files_name=($(/bin/find $log_files_path -maxdepth 1 -name "*.log" |grep -v error| awk -F/ '{ print $NF }'))
  13. #set the path to nginx.
  14. nginx_sbin="/usr/local/nginx/sbin/nginx"
  15. #Set how long you want to save
  16. save_days=15
  17. ############################################
  18. #Please do not modify the following script #
  19. ############################################
  20. mkdir -p $log_files_dir
  21. log_files_num=${#log_files_name[@]}

  22. #cut nginx log files
  23. for((i=0;i<$log_files_num;i++));do
  24. mv ${log_files_path}${log_files_name[i]} ${log_files_dir}$(date -d "yesterday" +"%Y%m%d_%s")_${log_files_name[i]}
  25. done
  26. #delete $save_days ago nginx log files
  27. find $log_files_path -mtime +$save_days -exec rm -rf {} \;
  28. #reload nginx
  29. $nginx_sbin -s reload
复制代码
------------------------
lnmp一键安装包太赞了。

[ 本帖最后由 phpbird 于 2017-1-4 18:33 编辑 ]
美国VPS推荐: 遨游主机LinodeLOCVPS主机云搬瓦工80VPSVultr美国VPS主机中国VPS推荐: 阿里云腾讯云。LNMP付费服务(代装/问题排查)QQ 503228080
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|VPS侦探 ( 鲁ICP备16040043号-1 )

GMT+8, 2024-9-28 03:29 , Processed in 0.025087 second(s), 17 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表