【CentOS 7LNMP架構25】,nginx日誌切割#180105

hellopasswdphp


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 in ls *.log do mv $log $log-$d done /bin/kill -HUP cat $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

將一個月以前的日誌做刪除mysql

[root@localhost vhost]# find /tmp/ -name *.log-* -type f -mtime +30 | xargs rm

mtime +30將一個月之前的日誌做刪除nginx

加入任務計劃sql

[root@localhost ~]# crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh

修改於180105shell

相關文章
相關標籤/搜索