Whatever I believed, I did; and whatever I did, I did with my whole heart and mind.
凡是我相信的,我都做了;凡是我做了的事,都是全身心地投入去做的
shell脚本如下
#!/bin/bash#nginx日志目录ngrizhi="/opt/nginx-1.24.0/logs/"#nginx日志名称ngmc="access.log"#备份旧日志cd $ngrizhi && mv $ngmc $ngmc.$(date '+%F')#重新生成日志,方式1:#nginx -s reopen#方式2:#kill -USR1 $(cat /var/run/nginx.pid)#方式3:kill -USR1 $(ps -ef|grep nginx |grep master | awk '{print $2}')#!/bin/bash #nginx日志目录 ngrizhi="/opt/nginx-1.24.0/logs/" #nginx日志名称 ngmc="access.log" #备份旧日志 cd $ngrizhi && mv $ngmc $ngmc.$(date '+%F') #重新生成日志,方式1: #nginx -s reopen #方式2: #kill -USR1 $(cat /var/run/nginx.pid) #方式3: kill -USR1 $(ps -ef|grep nginx |grep master | awk '{print $2}')#!/bin/bash #nginx日志目录 ngrizhi="/opt/nginx-1.24.0/logs/" #nginx日志名称 ngmc="access.log" #备份旧日志 cd $ngrizhi && mv $ngmc $ngmc.$(date '+%F') #重新生成日志,方式1: #nginx -s reopen #方式2: #kill -USR1 $(cat /var/run/nginx.pid) #方式3: kill -USR1 $(ps -ef|grep nginx |grep master | awk '{print $2}')
定时如下
每天00:00开始切割
[root@wj ~]# crontab -l00 00 * * * sh /root/cut_nginx.sh > /dev/null 2>&1[root@wj ~]# crontab -l 00 00 * * * sh /root/cut_nginx.sh > /dev/null 2>&1[root@wj ~]# crontab -l 00 00 * * * sh /root/cut_nginx.sh > /dev/null 2>&1
效果如下
[root@wj logs]# ll总用量 8-rw-r--r-- 1 nobody root 0 8月 30 00:00 access.log-rw-r--r-- 1 nobody root 0 8月 22 14:53 access.log.2024-08-22-rw-r--r-- 1 nobody root 0 8月 22 15:13 access.log.2024-08-23-rw-r--r-- 1 nobody root 0 8月 23 00:00 access.log.2024-08-24-rw-r--r-- 1 nobody root 0 8月 24 00:00 access.log.2024-08-25-rw-r--r-- 1 nobody root 0 8月 25 00:00 access.log.2024-08-26-rw-r--r-- 1 nobody root 0 8月 26 00:00 access.log.2024-08-27-rw-r--r-- 1 nobody root 0 8月 27 00:00 access.log.2024-08-28-rw-r--r-- 1 nobody root 0 8月 28 00:00 access.log.2024-08-29-rw-r--r-- 1 nobody root 0 8月 29 00:00 access.log.2024-08-30-rw-r--r-- 1 nobody root 1493 8月 22 14:53 error.log-rw-r--r-- 1 root root 5 8月 22 10:12 nginx.pid[root@wj logs]# ll 总用量 8 -rw-r--r-- 1 nobody root 0 8月 30 00:00 access.log -rw-r--r-- 1 nobody root 0 8月 22 14:53 access.log.2024-08-22 -rw-r--r-- 1 nobody root 0 8月 22 15:13 access.log.2024-08-23 -rw-r--r-- 1 nobody root 0 8月 23 00:00 access.log.2024-08-24 -rw-r--r-- 1 nobody root 0 8月 24 00:00 access.log.2024-08-25 -rw-r--r-- 1 nobody root 0 8月 25 00:00 access.log.2024-08-26 -rw-r--r-- 1 nobody root 0 8月 26 00:00 access.log.2024-08-27 -rw-r--r-- 1 nobody root 0 8月 27 00:00 access.log.2024-08-28 -rw-r--r-- 1 nobody root 0 8月 28 00:00 access.log.2024-08-29 -rw-r--r-- 1 nobody root 0 8月 29 00:00 access.log.2024-08-30 -rw-r--r-- 1 nobody root 1493 8月 22 14:53 error.log -rw-r--r-- 1 root root 5 8月 22 10:12 nginx.pid[root@wj logs]# ll 总用量 8 -rw-r--r-- 1 nobody root 0 8月 30 00:00 access.log -rw-r--r-- 1 nobody root 0 8月 22 14:53 access.log.2024-08-22 -rw-r--r-- 1 nobody root 0 8月 22 15:13 access.log.2024-08-23 -rw-r--r-- 1 nobody root 0 8月 23 00:00 access.log.2024-08-24 -rw-r--r-- 1 nobody root 0 8月 24 00:00 access.log.2024-08-25 -rw-r--r-- 1 nobody root 0 8月 25 00:00 access.log.2024-08-26 -rw-r--r-- 1 nobody root 0 8月 26 00:00 access.log.2024-08-27 -rw-r--r-- 1 nobody root 0 8月 27 00:00 access.log.2024-08-28 -rw-r--r-- 1 nobody root 0 8月 28 00:00 access.log.2024-08-29 -rw-r--r-- 1 nobody root 0 8月 29 00:00 access.log.2024-08-30 -rw-r--r-- 1 nobody root 1493 8月 22 14:53 error.log -rw-r--r-- 1 root root 5 8月 22 10:12 nginx.pid
工具Logrotate切割
安装软件
yum install logrotate -yyum install logrotate -yyum install logrotate -y
编写配置文件
[root@web-9 /etc/nginx]#cat /etc/logrotate.d/nginx/opt/nginx-1.24.0/logs/access.log {dailymissingokrotate 14notifemptycreate 640 root rootsharedscriptspostrotateif [ -f /opt/nginx-1.24.0/logs/nginx.pid ]; thenkill -USR1 `cat /opt/nginx-1.24.0/logs/nginx.pid`fiendscript}[root@web-9 /etc/nginx]#cat /etc/logrotate.d/nginx /opt/nginx-1.24.0/logs/access.log { daily missingok rotate 14 notifempty create 640 root root sharedscripts postrotate if [ -f /opt/nginx-1.24.0/logs/nginx.pid ]; then kill -USR1 `cat /opt/nginx-1.24.0/logs/nginx.pid` fi endscript }[root@web-9 /etc/nginx]#cat /etc/logrotate.d/nginx /opt/nginx-1.24.0/logs/access.log { daily missingok rotate 14 notifempty create 640 root root sharedscripts postrotate if [ -f /opt/nginx-1.24.0/logs/nginx.pid ]; then kill -USR1 `cat /opt/nginx-1.24.0/logs/nginx.pid` fi endscript }
参数解释
/var/log/nginx/*.log {dailymissingokrotate 14compressdelaycompressnotifemptycreate 640 root admsharedscriptspostrotate/usr/sbin/nginx -t && /usr/sbin/nginx -s reloadendscript}/var/log/nginx/*.log { daily missingok rotate 14 compress delaycompress notifempty create 640 root adm sharedscripts postrotate /usr/sbin/nginx -t && /usr/sbin/nginx -s reload endscript }/var/log/nginx/*.log { daily missingok rotate 14 compress delaycompress notifempty create 640 root adm sharedscripts postrotate /usr/sbin/nginx -t && /usr/sbin/nginx -s reload endscript }
daily: 每天进行一次日志切割。missingok: 如果日志文件丢失则忽略错误。rotate 14: 保留最近 14 天的日志文件。compress: 压缩已归档的日志文件。delaycompress: 在日志文件被切割后立即压缩。notifempty: 即使日志为空也要执行日志轮转。create 640 root adm: 当日志文件被创建时,设置权限为 640,所有者为 root,组为 adm。sharedscripts: 允许多个日志文件共享相同的脚本。postrotate: 在日志文件被切割后运行的脚本。/usr/sbin/nginx -t && /usr/sbin/nginx -s reload: 检查 Nginx 配置文件的有效性,并重新加载 Nginx 服务以确保新的日志文件路径被正确使用。daily: 每天进行一次日志切割。 missingok: 如果日志文件丢失则忽略错误。 rotate 14: 保留最近 14 天的日志文件。 compress: 压缩已归档的日志文件。 delaycompress: 在日志文件被切割后立即压缩。 notifempty: 即使日志为空也要执行日志轮转。 create 640 root adm: 当日志文件被创建时,设置权限为 640,所有者为 root,组为 adm。 sharedscripts: 允许多个日志文件共享相同的脚本。 postrotate: 在日志文件被切割后运行的脚本。 /usr/sbin/nginx -t && /usr/sbin/nginx -s reload: 检查 Nginx 配置文件的有效性,并重新加载 Nginx 服务以确保新的日志文件路径被正确使用。daily: 每天进行一次日志切割。 missingok: 如果日志文件丢失则忽略错误。 rotate 14: 保留最近 14 天的日志文件。 compress: 压缩已归档的日志文件。 delaycompress: 在日志文件被切割后立即压缩。 notifempty: 即使日志为空也要执行日志轮转。 create 640 root adm: 当日志文件被创建时,设置权限为 640,所有者为 root,组为 adm。 sharedscripts: 允许多个日志文件共享相同的脚本。 postrotate: 在日志文件被切割后运行的脚本。 /usr/sbin/nginx -t && /usr/sbin/nginx -s reload: 检查 Nginx 配置文件的有效性,并重新加载 Nginx 服务以确保新的日志文件路径被正确使用。
执行
#先检查配置文件是否正确[root@wj logrotate.d]# logrotate -d /etc/logrotate.d/nginx#开始执行分割[root@wj logrotate.d]# logrotate -f /etc/logrotate.d/nginx#先检查配置文件是否正确 [root@wj logrotate.d]# logrotate -d /etc/logrotate.d/nginx #开始执行分割 [root@wj logrotate.d]# logrotate -f /etc/logrotate.d/nginx#先检查配置文件是否正确 [root@wj logrotate.d]# logrotate -d /etc/logrotate.d/nginx #开始执行分割 [root@wj logrotate.d]# logrotate -f /etc/logrotate.d/nginx
Whatever I believed, I did; and whatever I did, I did with my whole heart and mind.
凡是我相信的,我都做了;凡是我做了的事,都是全身心地投入去做的
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容