#!/bin/bash # 监视$LOGPATH目录中的新文件,并将它们的内容输出到容器的标准输出 # $LOGPATH需要在pod中设置环境变量 lastfile="" inotifywait -mrq -e 'modify,moved_from,delete' $LOGPATH | while read path action file; do file_pid=$(ps -ef|grep "$path$file$"|grep -v grep|awk '{print $1}') # 添加新文件到tail遍历 if [[ "$action" == MODIFY ]] && [ -f "$path$file" ] && [ -z "$file_pid" ] && [[ "$lastfile" != "$path$file" ]]; then lastfile=$(echo $path$file|grep "log$") if [ -n "$lastfile" ]; then tail -F -n+1 "$lastfile" & fi # 清理已删除的文件tail进程 elif [[ "$action" == MOVED_FROM ]] || [[ "$action" == DELETE ]]; then kill -9 "$file_pid" fi done # 等待子进程退出以保持容器运行 wait