From 9a2703105e713a02d01a09884938593e3168251b Mon Sep 17 00:00:00 2001
From: PynnXie <71620349+PynnXie@users.noreply.github.com>
Date: Fri, 20 Nov 2026 16:38:53 +0800
Subject: [PATCH] Fix start.out rotation and preserve logs on restart (#1267)
---
.github/workflows/ci_build.yml | 6 ++-
Releases_Notes.md | 10 ++++
bin/startup.sh | 22 ++++----
...50\347\275\262\346\211\213\345\206\214.md" | 2 +
km-dist/README.md | 27 +++++++++-
km-dist/ReleaseKnowStreaming.xml | 5 ++
km-dist/conf/start-out.logrotate | 13 +++++
pom.xml | 2 +-
tests/start-out.sh | 54 +++++++++++++++++++
9 files changed, 125 insertions(+), 16 deletions(-)
create mode 100644 km-dist/conf/start-out.logrotate
create mode 100755 tests/start-out.sh
diff --git a/.github/workflows/ci_build.yml b/.github/workflows/ci_build.yml
index 2544c5706..389745409 100644
--- a/.github/workflows/ci_build.yml
+++ b/.github/workflows/ci_build.yml
@@ -13,6 +13,9 @@ jobs:
steps:
- uses: actions/checkout@v3
+
+ - name: Check startup log handling
+ run: bash tests/start-out.sh
- name: Set up JDK 11
uses: actions/setup-java@v3
@@ -37,7 +40,8 @@ jobs:
- name: Upload Binary Package
if: ${{ success() }}
- uses: actions/upload-artifact@v3
+ uses: actions/upload-artifact@v4
with:
name: KnowStreaming-${{ env.VERSION }}.tar.gz
path: km-dist/target/KnowStreaming-${{ env.VERSION }}.tar.gz
+ if-no-files-found: error
diff --git a/Releases_Notes.md b/Releases_Notes.md
index ce9a33d89..5e2b70b75 100644
--- a/Releases_Notes.md
+++ b/Releases_Notes.md
@@ -1,4 +1,14 @@
+## v3.4.2
+
+**Linux 安装包的控制台日志轮转**
+
+- 修复 `bin/startup.sh` 重启时清空 `logs/start.out` 的问题。启动命令和 Java 的标准输出、标准错误继续写入原路径,便于排查跨重启的问题。[#1267](https://github.com/didi/KnowStreaming/issues/1267)
+- 安装包新增 `conf/start-out.logrotate`。启用后每 5 分钟检查一次,按天归档,超过 100 MiB 可提前分卷。默认在轮转时清理超过 7 天的归档,最多保留 14 份压缩文件。管理员可按磁盘容量和日志量调整。
+- 已部署的实例可以单独安装轮转配置,运行中的 Java 进程无需重启。请按安装目录修改日志路径,并以服务账号启用定时任务。启动脚本的修改在下次启动时生效。[安装包配置说明](km-dist/README.md)
+
+---
+
## v3.4.0
diff --git a/bin/startup.sh b/bin/startup.sh
index cbde7c56b..48be7c29f 100644
--- a/bin/startup.sh
+++ b/bin/startup.sh
@@ -62,21 +62,17 @@ JAVA_OPT="${JAVA_OPT} --server.max-http-header-size=524288"
-if [ ! -d "${BASE_DIR}/logs" ]; then
- mkdir ${BASE_DIR}/logs
-fi
+LOG_DIR="${BASE_DIR}/logs"
+START_OUT="${LOG_DIR}/start.out"
-echo "$JAVA ${JAVA_OPT}"
+mkdir -p "${LOG_DIR}" || error_exit "Cannot create log directory ${LOG_DIR}"
-# check the start.out log output file
-if [ ! -f "${BASE_DIR}/logs/start.out" ]; then
- touch "${BASE_DIR}/logs/start.out"
-fi
-
-# start
-echo -e "---- 启动脚本 ------\n $JAVA ${JAVA_OPT}" > ${BASE_DIR}/logs/start.out 2>&1 &
+echo "$JAVA ${JAVA_OPT}"
+# Write the marker before the JVM starts so its first messages follow it.
+printf '%s\n' "---- 启动脚本 $(date '+%Y-%m-%d %H:%M:%S') ------" "$JAVA ${JAVA_OPT}" >> "${START_OUT}" \
+ || error_exit "Cannot write startup marker to ${START_OUT}"
-nohup $JAVA ${JAVA_OPT} >> ${BASE_DIR}/logs/start.out 2>&1 &
+nohup "$JAVA" ${JAVA_OPT} >> "${START_OUT}" 2>&1 &
-echo "${WEB_SERVER} is starting,you can check the ${BASE_DIR}/logs/start.out"
+echo "${WEB_SERVER} is starting,you can check the ${START_OUT}"
diff --git "a/docs/install_guide/\345\215\225\346\234\272\351\203\250\347\275\262\346\211\213\345\206\214.md" "b/docs/install_guide/\345\215\225\346\234\272\351\203\250\347\275\262\346\211\213\345\206\214.md"
index c42e63181..4c9ade2be 100644
--- "a/docs/install_guide/\345\215\225\346\234\272\351\203\250\347\275\262\346\211\213\345\206\214.md"
+++ "b/docs/install_guide/\345\215\225\346\234\272\351\203\250\347\275\262\346\211\213\345\206\214.md"
@@ -415,3 +415,5 @@ password: Didi_km_678
cd /data/KnowStreaming/bin/
sh startup.sh
```
+
+启动日志以追加方式写入 `/data/KnowStreaming/logs/start.out`。如需按天或按大小轮转该文件,可使用安装包内的 `conf/start-out.logrotate`,启用步骤见安装包根目录 `README.md` 的“start.out 日志轮转”一节([km-dist/README.md](../../km-dist/README.md))。
diff --git a/km-dist/README.md b/km-dist/README.md
index f5a717589..735b74887 100644
--- a/km-dist/README.md
+++ b/km-dist/README.md
@@ -9,6 +9,7 @@
- conf:配置
- application.yml:配置文件
- logback-spring.xml:日志配置
+ - start-out.logrotate:logs/start.out 的 logrotate 轮转配置(可选启用,见下文)
- helm:安装KnowStreaming的Helm相关文件
@@ -23,4 +24,28 @@
- ks-km.jar:KnowStreaming的Jar包
-- LICENSE:开源协议
\ No newline at end of file
+- LICENSE:开源协议
+
+
+## start.out 日志轮转(Linux)
+
+`bin/startup.sh` 将 Java 的标准输出和标准错误追加到 `logs/start.out`。每次启动都会写入时间和启动命令。已有实例也可以单独启用以下配置,运行中的 Java 进程无需重启。
+
+1. 确认 Linux 主机已安装 logrotate,并在 `conf/start-out.logrotate` 中填写实际日志文件的绝对路径。下面以安装目录 `/data/KnowStreaming` 为例。
+2. 以能读写日志文件和日志目录的服务账号执行以下命令。为这个实例使用单独的状态文件。按 `command -v` 的输出替换示例中的可执行文件路径。
+
+```sh
+command -v logrotate
+mkdir -p "$HOME/.local/state/knowstreaming" && chmod 700 "$HOME/.local/state/knowstreaming"
+/usr/sbin/logrotate -d -s "$HOME/.local/state/knowstreaming/start-out.status" "/data/KnowStreaming/conf/start-out.logrotate"
+```
+
+3. 预演通过后,在同一账号的 `crontab -e` 中添加以下一行,并确认该文件只由这一项任务轮转。保存配置文件后需要启用调度才会生效。
+
+```cron
+*/5 * * * * /usr/sbin/logrotate -s "$HOME/.local/state/knowstreaming/start-out.status" "/data/KnowStreaming/conf/start-out.logrotate"
+```
+
+任务每 5 分钟检查一次,按天归档。检查时超过 `maxsize 100M` 会提前分卷,两次检查之间文件仍可超过阈值。默认压缩归档,在轮转时清理超过 `maxage 7` 天的文件,最多保留 `rotate 14` 份。高日志量时,14 份可能覆盖不足 7 天。以上参数均可按需调整。
+
+`copytruncate` 让原进程继续写同一个文件,仍可用 `tail -f logs/start.out` 跟踪。复制与截短之间可能丢失少量输出。首次处理已有大文件时,请预留足以容纳一份完整副本的空间。
diff --git a/km-dist/ReleaseKnowStreaming.xml b/km-dist/ReleaseKnowStreaming.xml
index 37833d4b2..0a6192287 100755
--- a/km-dist/ReleaseKnowStreaming.xml
+++ b/km-dist/ReleaseKnowStreaming.xml
@@ -59,6 +59,11 @@
README.md
.
+
+ conf/start-out.logrotate
+ conf
+ 0644
+
../LICENSE
.
diff --git a/km-dist/conf/start-out.logrotate b/km-dist/conf/start-out.logrotate
new file mode 100644
index 000000000..dd10097ad
--- /dev/null
+++ b/km-dist/conf/start-out.logrotate
@@ -0,0 +1,13 @@
+# Set the absolute path for this installation. See README.md for scheduling.
+"/data/KnowStreaming/logs/start.out" {
+ daily
+ maxsize 100M
+ rotate 14
+ maxage 7
+ dateext
+ dateformat -%Y%m%d-%s
+ compress
+ copytruncate
+ missingok
+ notifempty
+}
diff --git a/pom.xml b/pom.xml
index 0667c7535..324b3b209 100644
--- a/pom.xml
+++ b/pom.xml
@@ -15,7 +15,7 @@
- 3.4.0
+ 3.4.2
8
8
diff --git a/tests/start-out.sh b/tests/start-out.sh
new file mode 100755
index 000000000..bb188b8e3
--- /dev/null
+++ b/tests/start-out.sh
@@ -0,0 +1,54 @@
+#!/usr/bin/env bash
+set -eu
+
+root=$(cd "$(dirname "$0")/.." && pwd)
+work=$(mktemp -d)
+trap 'rm -rf "$work"' EXIT
+export JAVA_HOME="$work/jdk"
+mkdir -p "$work/bin" "$work/logs" "$JAVA_HOME/bin"
+cp "$root/bin/startup.sh" "$work/bin/"
+cat > "$JAVA_HOME/bin/java" <<'JAVA'
+#!/bin/sh
+if [ "$1" = "-version" ]; then
+ echo 'openjdk version "11.0.0"' >&2
+ exit 0
+fi
+echo "stdout $LAUNCH"
+echo "stderr $LAUNCH" >&2
+touch "$DONE"
+JAVA
+chmod +x "$JAVA_HOME/bin/java"
+
+log="$work/logs/start.out"
+echo 'prior history' > "$log"
+for launch in first second; do
+ LAUNCH="$launch" DONE="$work/$launch.done" JAVA_OPT="-Dtest.launch=$launch" \
+ bash "$work/bin/startup.sh" > /dev/null
+ for ((i=0; i<100; i++)); do
+ [ -e "$work/$launch.done" ] && break
+ sleep 0.05
+ done
+ test -f "$work/$launch.done" || { echo "Mock JVM did not finish: $launch" >&2; exit 1; }
+done
+
+# Keep just the launch command and the two streams from each JVM.
+awk '/-Dtest[.]launch=first/ {print "start first"}
+ /-Dtest[.]launch=second/ {print "start second"}
+ /^(prior history|stdout |stderr )/ {print}' "$log" > "$work/observed"
+diff -u - "$work/observed" <<'EXPECTED'
+prior history
+start first
+stdout first
+stderr first
+start second
+stdout second
+stderr second
+EXPECTED
+
+mv "$log" "$work/saved.log"
+mkdir "$log" # An unwritable file target, even when the test runs as root.
+if LAUNCH=failed DONE="$work/failed.done" bash "$work/bin/startup.sh" > /dev/null 2>&1; then
+ echo 'Startup reported success with an unwritable log target' >&2
+ exit 1
+fi
+echo 'start.out regression checks passed'