在 CentOS 上使用 nohup 时“日志没有生成”是常见问题,通常不是 nohup 本身坏了,而是命令写法、路径、权限或程序行为导致的。下面按最常见原因逐一说明。
很多人这样写:
nohup python app.py &
但 nohup 默认只会把 stdout 写到 nohup.out,而且:
✅ 正确写法:
nohup python app.py > app.log 2>&1 &
nohup: failed to open 'nohup.out': Permission denied
CentOS 中:
/root 下普通用户不能写✅ 解决:
cd /tmp
nohup xxx > /tmp/app.log 2>&1 &
或指定绝对路径日志:
nohup ./app > /var/log/app.log 2>&1 &
如果程序启动即崩溃:
✅ 检查:
ps -ef | grep app
✅ 看是否有报错:
tail -f app.log
| 情况 | 日志行为 |
|---|---|
| 没重定向 | 写 nohup.out(当前目录) |
| 重定向 stdout | 只写 stdout |
| 重定向 2>&1 | stdout + stderr 都写 |
| 程序无输出 | 日志为空 |
如果你是通过 SSH 登录后 nohup,会话结束可能:
✅ 更稳定做法:
setsid nohup python app.py > app.log 2>&1 &
CentOS 默认开启 SELinux,可能阻止写日志。
✅ 排查:
getenforce
✅ 临时测试:
setenforce 0
nohup your_command > /path/to/log/app.log 2>&1 &
或更好:
setsid your_command > /path/to/log/app.log 2>&1 < /dev/null &
你可以贴出:
cat /etc/redhat-release)我可以直接告诉你为什么没生成、该怎么改。