Skip to content

Instantly share code, notes, and snippets.

@stilleshan
Last active June 3, 2021 04:16
Show Gist options
  • Save stilleshan/885bf7de23381daa5b89552e14ca619e to your computer and use it in GitHub Desktop.
Save stilleshan/885bf7de23381daa5b89552e14ca619e to your computer and use it in GitHub Desktop.
Nginx 日志清理脚本
#!/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