温馨提示×

为何nohup日志没有生成

小樊
49
2026-09-18 14:30:45
栏目: 编程语言

nohup 日志没有生成,常见原因和排查方法如下:


一、最常见的几种原因

1️⃣ 没有重定向输出

nohup 默认只保证进程不挂断,日志是否生成取决于你有没有重定向:

nohup command &

这样不会生成 nohup.out(某些 shell 不会自动创建)。

✅ 正确写法:

nohup command > nohup.out 2>&1 &

或分开:

nohup command > out.log 2> err.log &

2️⃣ 程序本身没有任何输出

如果程序:

  • 没有 print / echo / log
  • 输出被缓冲(Python 常见)

那么日志文件即使创建了,也可能是 0 字节。

✅ 解决:

  • Python 加 -u
nohup python -u app.py > log.out 2>&1 &
  • 或程序中强制 flush

3️⃣ 当前目录没有写权限

nohup.out 默认写在当前目录

pwd
ls -ld .

如果无写权限:

nohup: ignoring input and appending output to 'nohup.out'
nohup: failed to open 'nohup.out': Permission denied

✅ 解决:

nohup command > /tmp/app.log 2>&1 &

4️⃣ 程序瞬间退出

进程启动后马上报错退出,日志可能:

  • 没来得及写
  • 在 stderr 而不是 stdout

✅ 排查:

jobs
ps -ef | grep command

并查看:

cat nohup.out

5️⃣ 使用了 systemd / 容器 / 虚拟环境

  • Docker:日志在 docker logs
  • systemd:日志在 journalctl
  • supervisor:日志在配置目录

nohup 在这些环境里常常“看似无效”。


二、快速自检清单 ✅

nohup your_cmd > run.log 2>&1 &
sleep 2
ls -l run.log
cat run.log
ps -ef | grep your_cmd

三、推荐稳妥写法(生产)

nohup your_command >> /var/log/app.log 2>&1 &
disown

如果你愿意,可以把:

  • 你用的命令
  • 操作系统
  • 是否 Docker / systemd

发出来,我可以直接帮你定位。

0 踩