split_nginx_log.sh

#!/bin/bash

logs_path="/var/log/nginx/"
mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log
systemctl stop nginx
sleep 1
systemctl start nginx
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

验证:

[root@logstash ~]# ps -ef |grep nginx
root       5083      1  0 10:11 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      5084   5083  0 10:11 ?        00:00:00 nginx: worker process
nginx      5085   5083  0 10:11 ?        00:00:00 nginx: worker process
nginx      5086   5083  0 10:11 ?        00:00:00 nginx: worker process
nginx      5087   5083  0 10:11 ?        00:00:00 nginx: worker process
root       5266   1586  0 10:13 pts/0    00:00:00 grep --color=auto nginx
[root@logstash ~]# sh split_nginx_log.sh 
[root@logstash ~]# ps -ef |grep nginx
root       5293      1  0 10:13 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      5294   5293  0 10:13 ?        00:00:00 nginx: worker process
nginx      5295   5293  0 10:13 ?        00:00:00 nginx: worker process
nginx      5296   5293  0 10:13 ?        00:00:00 nginx: worker process
nginx      5297   5293  0 10:13 ?        00:00:00 nginx: worker process
root       5300   1586  0 10:13 pts/0    00:00:00 grep --color=auto nginx
[root@logstash ~]# 
[root@logstash nginx]# ls -l access_*
-rw-r--r-- 1 root root 0 Jun 25 10:11 access_20240624.log
[root@logstash nginx]#
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.