在 CentOS 上使用 nohup 时,日志文件为空通常不是 nohup 本身的问题,而是程序行为或重定向方式导致的。下面按常见原因逐一说明。
很多程序:
✅ 验证方法:
nohup your_command > out.log 2>&1 &
tail -f out.log
如果确实没内容,说明程序本身没写 stdout/stderr。
Python / Java / C 程序经常有输出缓冲:
print() 不会立刻写文件✅ 解决方案:
nohup python -u app.py > out.log 2>&1 &
nohup java -Djava.util.logging.manager=... > out.log 2>&1 &
或加:
stdbuf -oL nohup your_command > out.log 2>&1 &
错误示例(日志一定为空):
nohup your_command &
> out.log
正确写法:
nohup your_command > out.log 2>&1 &
一些程序会:
/var/log/xxx.log例如:
✅ 查看:
ps -ef | grep your_command
nohup.out 或你指定的 log 文件:
✅ 检查:
jobs
ps -ef | grep your_command
& 但 shell 退出太快在 非登录 shell / 脚本中可能出问题。
更稳妥方式:
setsid nohup your_command > out.log 2>&1 &
# 1. 手动前台运行,看是否有输出
your_command
# 2. 前台 + 重定向
your_command > test.log 2>&1
cat test.log
# 3. 再 nohup
nohup your_command > out.log 2>&1 &
你可以告诉我:
nohup.out 还是自定义日志我可以直接帮你定位到哪一行导致日志为空。