在 Linux 中使用 nohup 时,日志“丢失”通常不是真的被删除了,而是被重定向、缓冲、覆盖或写到了你没注意的地方。下面按常见原因逐一说明,并给出解决办法。
nohup 默认只把 stdout 写到 nohup.outnohup python app.py &
等价于:
nohup python app.py > nohup.out 2>&1 &
但很多人会误以为:
⚠️ 问题点:
os.chdir()),nohup.out 会在新目录nohup 会把日志写到 ~/$HOME/nohup.out✅ 解决:
nohup python app.py > /var/log/app.log 2>&1 &
如果你多次启动:
nohup app > app.log 2>&1 &
nohup app > app.log 2>&1 &
第二次会 清空 app.log
✅ 使用追加:
>> app.log 2>&1
很多程序(Python / Java / Node):
logging结果:
nohup.out 是空的✅ 检查:
lsof -p <pid> | grep log
print("hello")
在 nohup 下:
✅ 解决:
python -u app.py
或
print("x", flush=True)
nohup 不保证持久化✅ 更可靠方案:
systemdsupervisord常见隐藏位置:
./nohup.out~/nohup.out/dev/null(如果你写错了)检查:
ls -l nohup.out ~/nohup.out
例如:
nohup app > /dev/null 2>&1 &
那当然“没有日志”。
nohup python -u app.py >> /var/log/app.log 2>&1 &
或更专业:
systemd-run --unit=app.service python app.py
如果你愿意,可以贴出:
我可以直接帮你定位“日志去哪了”。