nohup 日志不输出,通常不是 nohup 本身的问题,而是输出缓冲、重定向方式或程序行为导致的。下面按常见原因逐一说明,并给出解决办法。
nohup.out 文件存在但为空或很久才更新一次nohup 默认会把 stdout/stderr 重定向到 nohup.outnohup python test.py &
print() 的内容不会立刻出现。
nohup python -u test.py > nohup.out 2>&1 &
print("hello", flush=True)
stderr,但没被重定向nohup command > nohup.out 2>&1 &
或分开:
nohup command > stdout.log 2> stderr.log &
lsof -p <pid> | grep log
或检查程序日志配置。
nohup.out 会生成在启动时的当前目录find / -name nohup.out 2>/dev/null
或指定日志路径(推荐):
nohup command > /var/log/my.log 2>&1 &
nohup.out 为空nohup command > my.log 2>&1 &
cat my.log
或前台先跑一次看是否报错。
nohup command & > nohup.out
nohup command > nohup.out 2>&1 &
df -h
df -i
ls -l nohup.out
nohup your_command > app.log 2>&1 &
查看实时日志:
tail -f app.log
| 问题 | 解决 |
|---|---|
| 日志不更新 | 程序输出缓冲 |
| 没报错 | 没重定向 stderr |
| 日志为空 | 程序没输出 / 已退出 |
| 找不到日志 | 目录不对 |
| Python 无输出 | 用 python -u |
如果你愿意,可以把具体命令 / 程序语言 / 启动方式发出来,我可以直接帮你定位是哪一类问题。