nginx日志分割脚本

时间:2023-03-08 16:01:21

[root@localhost nginx]# cat logs/nginx.pid
5118
[root@localhost nginx]# kill -QUIT 5118
-QUIT : 关闭进程
-HUP : 重载服务, 平滑重启
-USR1: 日志切换,(通过结合crontab,每天生成一个新的日志)
-USR2: 平滑升级可执行程序
=[root@localhost ~]# cat /opt/fenge-nginx.sh
#!/bin/bash
#fenge nginx logs
savepath_log='/home/nginx/logs'
nglogs='/usr/local/nginx/logs'
[ -d $savepath_log ] || mkdir -p $savepath_log
[ -d $savepath_log/$(date +%Y)/$(date +%m) ] || mkdir -p  $savepath_log/$(date +%Y)/$(date +%m)
mv $nglogs/access.log $savepath_log/$(date +%Y)/$(date +%m)/access.$(date +%Y%m%d).log
mv $nglogs/error.log $savepath_log/$(date +%Y)/$(date +%m)/error.$(date +%Y%m%d).log
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)
---------------------------------------------------------------------------
[root@localhost ~]# chmod +x /opt/fenge-nginx.sh
[root@localhost ~]# /opt/fenge-nginx.sh
[root@localhost ~]# tree /home/nginx/
/home/nginx/
└── logs
    └── 2014
        └── 12
            ├── access.20141220.log
            └── error.20141220.log
3 directories, 2 files
-----------------------------------------------------
[root@localhost ~]# crontab  -l
0 0 * * * /opt/fenge-nginx.sh