写一个nginx日志切割shell脚本,保留30天的文件
时间: 2024-02-01 10:02:49 浏览: 96
好的,以下是答案:
您可以使用以下Shell命令编写一个nginx日志切割脚本,以保留30天的文件:
```shell
#!/bin/bash
LOG_DIR=/var/log/nginx
OLDLOG_DIR=/var/log/nginx/oldlogs
COMPRESSION_TOOL='gzip'
find ${LOG_DIR} -type f -iname "access.log*" -mtime +29 -delete
mv ${LOG_DIR}/access.log ${OLDLOG_DIR}/access-$(date +%Y%m%d%H%M%S).log
kill -USR1 $(cat /var/run/nginx.pid)
cd ${OLDLOG_DIR}
for i in $(ls -1 access*log); do
${COMPRESSION_TOOL} $i
done
```
这个脚本将删除30天前的所有访问日志文件并将当前的访问日志文件从`${LOG_DIR}/access.log`移动到`${OLDLOG_DIR}`,并触发Nginx重新打开日志文件。随后,它将所有已轮换的日志文件压缩。
阅读全文
相关推荐







