在 CentOS 上使用 nohup 时,日志不写入文件通常有下面几种常见原因,你可以逐一排查。
nohup command &
默认会写入:
nohup.out # 当前目录
如果没有写权限,会尝试:
$HOME/nohup.out
nohup python app.py &
✅ 正确写法(推荐):
nohup python app.py > app.log 2>&1 &
nohup python app.py > app.log &
如果程序报错,错误在 stderr,不会进 app.log
✅ 正确:
2>&1
很多程序:
✅ 排查:
strace -f -e write nohup your_command
Python / Java / C++ 常见
nohup python app.py > app.log &
Python 在非 TTY 下:
print 是 块缓冲✅ 解决:
python -u app.py
或:
stdbuf -oL python app.py
ps -ef | grep app.py
如果没有进程:
✅ 检查:
cat nohup.out
nohup your_command > run.log 2>&1 &
查看:
tail -f run.log
getenforce
如果是 Enforcing,可能限制写文件
✅ 临时测试:
setenforce 0
mount | grep "on / "
systemctl start your.service
journalctl -u your.service -f
tmux new -s app
> log 2>&1-u 或 stdbuf如果你愿意,可以把:
贴出来,我可以直接帮你定位是哪一条原因。