温馨提示×

如何查看nohup日志输出

小樊
60
2026-08-20 10:18:25
栏目: 编程语言

nohup 命令默认会把输出重定向到当前目录下的 nohup.out 文件。查看方式取决于你启动程序时的具体写法。


一、默认情况(最常见)

nohup python app.py &

输出文件:

nohup.out

查看方式:

# 查看全部
cat nohup.out

# 实时查看(最常用)
tail -f nohup.out

# 查看最后 100 行
tail -n 100 nohup.out

二、手动指定了输出文件

如果你是这样启动的:

nohup python app.py > app.log 2>&1 &

那么日志在:

app.log

查看方式:

tail -f app.log

说明:

  • > app.log:标准输出
  • 2>&1:错误输出也写入同一个文件

三、同时输出到多个地方(不常见)

nohup python app.py > stdout.log 2> stderr.log &

查看:

tail -f stdout.log
tail -f stderr.log

四、查看进程是否在运行

ps -ef | grep app.py

或:

jobs -l

五、常见问题

1️⃣ 找不到 nohup.out?

可能原因:

  • 文件不在当前目录(启动时目录不同)
  • 输出被重定向了
  • 程序还没输出内容

查找:

find / -name nohup.out 2>/dev/null

2️⃣ nohup.out 太大怎么办?

# 清空文件(不删文件)
: > nohup.out

或用 logrotate 做日志切割(生产环境推荐)。


3️⃣ 想实时看日志并退出终端?

tail -f nohup.out

Ctrl + C 只是退出 tail,不会影响程序运行。


如果你愿意,可以把你具体的 nohup 启动命令贴出来,我可以帮你精确判断日志在哪、怎么看最合适。

0