nohup 日志不更新,通常不是 nohup 本身的问题,而是输出缓冲/重定向/进程状态导致的。下面按最常见原因给你排查思路。
nohup.out 文件存在tail -f nohup.out 很久没变化很多程序(尤其是 Python、Java、C/C++)默认会缓冲 stdout,而不是实时写文件。
例如 Python:
nohup python app.py &
stdout 是块缓冲,只有缓冲区满或程序退出才写文件。
nohup python -u app.py > nohup.out 2>&1 &
或:
PYTHONUNBUFFERED=1 nohup python app.py &
print("xxx", flush=True)
ls -l nohup.out
lsof -p <pid> | grep -E '1w|2w'
可能情况:
✅ 确认程序真正写的是哪个 fd
ps -ef | grep your_program
tail -n 50 nohup.out
常见情况:
find / -name nohup.out 2>/dev/null
df -h
df -i
nohup python app.py > nohup.out &
如果程序输出到 stderr,你看不到。
nohup python app.py > nohup.out 2>&1 &
有些程序:
这时 nohup.out 不会再更新。
✅ 用:
strace -p <pid> -e write
确认是否真的在写。
你可以按顺序执行:
# 1. 进程还在吗
ps -ef | grep your_program
# 2. 文件在增长吗
ls -l nohup.out
# 3. 是不是缓冲问题
python -u 或 flush
# 4. 是不是写到别的地方
lsof -p <pid>
nohup 日志不更新,90% 是因为 stdout 缓冲,不是 nohup 的问题。
如果你愿意,可以把:
tail -f nohup.out 的现象贴出来,我可以直接帮你定位是哪一条。