在 CentOS 上,nohup 启动的程序日志不更新,通常不是 nohup 本身的问题,而是输出缓冲、文件重定向或进程状态导致的。下面按常见原因给你系统排查一下。
nohup.out 文件存在很多程序(尤其是 Python、Java、C/C++)默认使用块缓冲(block buffering),而不是行缓冲。
如果是你自己的程序:
print("xxx", flush=True)
或
import sys
sys.stdout.flush()
fflush(stdout);
stdbuf 关闭缓冲(最常用)nohup stdbuf -oL -eL your_command > nohup.out 2>&1 &
说明:
-oL:stdout 行缓冲-eL:stderr 行缓冲适合:
nohup ./app > nohup.out &
此时:
nohup 仍可能写 nohup.outnohup ./app > nohup.out 2>&1 &
或明确指定:
nohup ./app > my.log 2>&1 &
lsof -p <pid> | grep log
查看进程真正在写哪些文件。
df -h
df -i
lsof | grep deleted
如果 nohup.out 被删除:
cp /proc/<pid>/fd/1 restore.log
ps -ef | grep your_program
strace -p <pid>
如果你是在 systemd 服务 / cron / ansible 里跑:
✅ 建议:
stdbuf你可以按顺序确认:
nohup xxx > log 2>&1 &stdbuf -oLlsof -p pid 看实际写哪个文件✅ 不要依赖 nohup.out
nohup stdbuf -oL ./app > app.log 2>&1 &
✅ 或直接写日志文件
./app >> app.log 2>&1
✅ 长期运行建议用 systemd
如果你愿意,可以把:
贴出来,我可以给你一个完全针对你场景的写法。