shallow丿ove
nginx日志切割
- 自定义shell脚本
- vi /usr/local/sbin/nginx_log_rotate.sh
#!/bin/bash
##假设nginx的日志存放路径为/data/logs/
d=
date -d "-1 day" +%Y%m%d
logdir="/data/logs" nginx_pid="/usr/local/nginx/logs/nginx.pid cd $logdir for log inls *.log
do mv $log $log-$d done /bin/kill -HUPcat $nginx_pid
- crontab -e 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
[root@localhost vhost]# vi /usr/local/sbin/nginx_logrotate.sh
1 #!/bin/bash
2 d=`date -d "-1 day" +%Y%m%d`
3 logdir="/tmp/"
4 nginx_pid="/usr/local/nginx/logs/nginx.pid"
5 cd $logdir
6 for log in `ls *.log`
7 do
8 mv $log $log-$d
9 done
10 /bin/kill -HUP `cat $nginx_pid`
[root@localhost vhost]# sh -x /usr/local/sbin/nginx_logrotate.sh
++ date -d '-1 day' +%Y%m%d
+ d=20180103
+ logdir=/tmp/
+ nginx_pid=/usr/local/nginx/logs/nginx.pid
+ cd /tmp/
++ ls test.com.log
+ for log in '`ls *.log`'
+ mv test.com.log test.com.log-20180103
++ cat /usr/local/nginx/logs/nginx.pid
+ /bin/kill -HUP 5676
[root@localhost vhost]# ls /tmp
mysql.sock pear php-fcgi.sock test.com.log test.com.log- test.com.log-20180103
[root@localhost vhost]# crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
将一个月之前的日志作删除
[root@localhost vhost]# find /tmp/ -name *.log-* -type f -mtime +30 | xargs rm
mtime +30将一个月以前的日志作删除
加入任务计划
[root@localhost ~]# crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh