时间越久内存占用率越高,高并发量,会给服务器带来巨大的缓存压力
解决方法:创建一个新的目录,设置周期性计划定期的将旧目录里面的日志移动到新目录中,一般只保存30天内,30天后一律删除
[[email protected] ~]# date "+%Y%m%d" //今天的时间
20190913
[[email protected] ~]# date -d "-1 day" "+%Y%m%d" //昨天的时间
20190912
=====================================================================================
[[email protected] ~]# vim /opt/cut_nginx_log.sh
#!/bin/bash #cut_nginx_log.sh datetime=$(date -d "-1 day" "+%Y%m%d") //时间,date -d "-1 day"代表日期减1 log_path="/usr/local/nginx/logs" //日志的存放位置 pid_path="/usr/local/nginx/logs/nginx.pid" //进程的PID号,有PID号代表 进程还活着 [ -d $log_path/backup ] || mkdir -p $log_path/backup //如果$log_path/backup不是一个目录则创建$log_path/backup目录 if [ -f $pid_path ] //如果$pid_path是一个文件 then mv $log_path/access.log $log_path/backup/access.log-$datetime //那么移动旧日志的文件到新目录下以此来实现分割 kill -USR1 $(cat $pid_path) //USR1代表信号,他会给进程传一个信号让进程生成一个新的日志 find $log_path/backup -mtime +30 | xargs rm -f //查找30天前的日志并删除 else echo "Error,Nginx is not working!" | tee -a /var/log/messages //否则提示nginx is not working且追加到日志中 fi
[[email protected] ~]# chmod +x /opt/cut_nginx_log.sh
[[email protected] ~]# bash /opt/cut_nginx_log.sh
[[email protected] ~]# cd /usr/local/nginx/logs/
[[email protected] logs]# ls
access.log backup error.log nginx.pid
[[email protected] logs]# ls backup/
access.log-20190912
[[email protected] ~]# crontab -e
crontab: installing new crontab
[[email protected] ~]# crontab -l
0 0 * * * bash /opt/cut_nginx_log.sh //每月每周每天的0时0分执行脚本
当未运行时!
[[email protected] logs]# killall -9 nginx
[[email protected] logs]# bash /opt/cut_nginx_log.sh
/opt/cut_nginx_log.sh: 第 11 行:kill: (8282) - 没有那个进程
[[email protected] logs]# rm -rf /usr/local/nginx/logs/nginx.pid
[[email protected] logs]# bash /opt/cut_nginx_log.sh
Error,Nginx is not working!
原文地址:https://www.cnblogs.com/cxm123123form/p/11516088.html