Last active
June 3, 2021 04:16
-
-
Save stilleshan/885bf7de23381daa5b89552e14ca619e to your computer and use it in GitHub Desktop.
Nginx 日志清理脚本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin | |
export PATH | |
# 修改以下最大单个日志文件尺寸和日志路径变量. | |
# 根据需求自定计划任务运行. | |
# 当日志文件尺寸大于 MAX_SIZE 则删除 10000 行 (可自行修改),循环检查尺寸并环删除行,直到尺寸小于 MAX_SIZE 结束. | |
MAX_SIZE=100000000 | |
# 100M | |
LOG_PATH=/root/dnmp/logs/nginx | |
# LOG_PATH=/home/wwwlogs | |
# 日志路径 | |
for LOG in $(ls -1 ${LOG_PATH}/*.log) | |
do | |
SIZE=`ls -l $LOG | awk '{print $5}'` | |
while [ $SIZE -gt $MAX_SIZE ] | |
do | |
sed -i '1,100000d' $LOG | |
SIZE=`ls -l $LOG | awk '{print $5}'` | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment