nginx日志切割脚本
#!/bin/bash
#Nginx 日志文件存放的路径
nginx_access_log="/usr/local/openresty/nginx/logs/access"
nginx_error_log="/usr/local/openresty/nginx/logs/error"
#备份存放的路径
access_log_path="/data/nginx-logs/access/"
error_log_path="/data/nginx-logs/error/"
# backup access_log_path
if [ ! -e ${access_log_path} ]
then
mkdir -p ${access_log_path}
fi
# backup error_log_path
if [ ! -e ${error_log_path} ]
then
mkdir -p ${error_log_path}
fi
#访问日志备份
backup_access="${access_log_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/$(date -d "yesterday" +"%d")"
if [ ! -e ${backup_access} ]
then
mkdir -p ${backup_access}
fi
mv ${nginx_access_log}/* ${backup_access}
#错误日志备份
backup_error="${error_log_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/$(date -d "yesterday" +"%d")"
if [ ! -e ${backup_error} ]
then
mkdir -p ${backup_error}
fi
mv ${nginx_error_log}/* ${backup_error}
#重新生成日志
kill -USR1 `cat /usr/local/openresty/nginx/logs/nginx.pid`
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
上次更新: 2022/10/08, 16:35:41