Last active
March 9, 2022 06:24
-
-
Save azhai/fe569a3b38324b7a68379361cd7eb88b to your computer and use it in GitHub Desktop.
用于按天切割supervisor管理或任务进程日志
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
#用于按天切割supervisor管理或任务进程日志 | |
#0 0 * * * /bin/gawk -f /opt/scripts/logcut.awk /var/log/supervisor/*.log | |
BEGIN { | |
"date -d yesterday +%Y-%m-%d" | getline yesterday #零点执行,获取昨天的日期 | |
} | |
$1 <= yesterday { | |
positions[FILENAME] = FNR #记录每个日志文件读取到哪里 | |
day = $1 | |
gsub(/\-/, "", day) #本行日志属于哪一天 | |
newfname = FILENAME "-" day | |
print $0 >> newfname #将本行日志复制到新文件 | |
} | |
END { | |
for (fname in positions) { #记录了每个日志文件已切割的行数 | |
pos = positions[fname] | |
if (pos > 0) { | |
system("/bin/sed -i '1," pos "d' " fname) #清除已切割的旧日志 | |
system("/bin/gzip -q " fname "-*") #压缩文件,会自动跳过.gz结尾的文件 | |
system("/bin/chown --reference=" fname " " fname "-*.gz") #修改新文件的拥有者 | |
} | |
} | |
system("/bin/supervisorctl reload") #重启supervisor任务,继续写日志文件 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment