nohup 日志不输出,通常不是 nohup 本身的问题,而是输出被缓冲、重定向写错、或进程提前退出导致的。下面按常见原因和解决办法给你梳理一下。
最稳妥的写法:
nohup your_command > output.log 2>&1 &
含义:
> output.log:标准输出写文件2>&1:错误输出也写同一个文件&:后台运行⚠️ 很多人只写:
nohup your_command &
这样日志可能:
nohup.out很多程序(尤其是 Python、Java、C++)默认缓冲输出,不立即写文件。
✅ Python 示例解决:
nohup python -u main.py > output.log 2>&1 &
或:
nohup stdbuf -oL python main.py > output.log 2>&1 &
-u:关闭缓冲stdbuf -oL:行缓冲检查:
ls -lh nohup.out
ls -lh output.log
查看内容:
tail -f output.log
# 或
cat nohup.out
检查进程:
ps -ef | grep your_command
或:
jobs
如果进程不在了:
❌ 错误示例:
nohup your_command 2>&1 > output.log
✅ 正确顺序:
nohup your_command > output.log 2>&1
在容器里更推荐:
your_command >> output.log 2>&1 &
或:
setsid your_command > output.log 2>&1 &
先用前台跑一次:
your_command
确认:
再决定怎么用 nohup。
nohup stdbuf -oL your_command > run.log 2>&1 &
tail -f run.log
如果你愿意,可以把:
发出来,我可以直接帮你改好命令。