在 CentOS(以及其他 Linux 系统)上,nohup 命令启动的进程日志不实时刷新,通常不是 nohup 本身的问题,而是标准输出缓冲机制导致的。
程序输出到终端时是行缓冲(line-buffered),一行就刷一次;
但输出到文件(如 nohup.out)时是全缓冲(block-buffered),只有缓冲区满或程序退出才写入磁盘。
nohup python app.py > nohup.out 2>&1 &
nohup.out 不会实时刷新tail -f nohup.out 看起来“卡住”了-u 参数(推荐)nohup python -u app.py > nohup.out 2>&1 &
-u 表示 无缓冲(unbuffered)
export PYTHONUNBUFFERED=1
nohup python app.py > nohup.out 2>&1 &
JVM 默认也是缓冲的:
nohup java -Djava.util.logging.SimpleFormatter.buffer=false \
-jar app.jar > nohup.out 2>&1 &
或使用 log4j / logback 并配置 immediateFlush=true
setvbuf(stdout, NULL, _IONBF, 0);
或在运行前:
stdbuf -o0 nohup ./app > nohup.out 2>&1 &
nohup bash -u script.sh > nohup.out 2>&1 &
或在脚本里:
exec > >(stdbuf -o0 tee nohup.out)
tail -f 看tail -f nohup.out
print(..., flush=True)System.out.flush()nohup 日志不刷新,99% 是因为程序输出被缓冲了,不是 nohup 的问题。
✅ 最快解决:
nohup python -u xxx.py > nohup.out 2>&1 &
如果你能告诉我具体是什么程序(Python / Java / Shell / 其他),我可以给你更精确的命令。