温馨提示×

centos nohup日志如何查找错误

小樊
61
2026-07-09 03:19:31
栏目: 智能运维

CentOS 下使用 nohup 启动程序时,错误日志主要看 nohup.out 以及你是否重定向了输出。下面按常见场景说明如何查找错误。


一、默认情况:nohup.out

nohup java -jar app.jar &

默认日志文件:

nohup.out

(在当前工作目录)

查看错误

tail -f nohup.out

或查看最近错误:

tail -n 200 nohup.out

搜索错误关键字:

grep -i error nohup.out
grep -i exception nohup.out

二、输出被重定向的情况(最常见)

很多人会这样写:

nohup java -jar app.jar > app.log 2>&1 &

此时:

  • stdout + stderr 都写入 app.log
  • nohup.out 不会生成

查看错误

tail -f app.log
grep -i error app.log
grep -i exception app.log

三、stdout 和 stderr 分开的情况

nohup java -jar app.jar > out.log 2> err.log &

错误日志在:

err.log

查看:

tail -f err.log

四、程序已经运行,如何找日志文件?

1️⃣ 通过进程查看

ps -ef | grep app.jar

或:

jps -l

2️⃣ 查看进程打开的文件

lsof -p PID

重点看:

  • cwd:当前目录(nohup.out 可能在这里)
  • 1w / 2w:stdout / stderr 输出文件

五、常见错误排查技巧

1️⃣ 程序根本没启动成功

ps -ef | grep app.jar

没有进程说明启动失败,错误可能在:

  • 你重定向的日志
  • 或终端输出(如果你没重定向)

2️⃣ 权限问题

ls -l nohup.out
chmod 755 app.jar

3️⃣ Java / Python 程序常见错误

Java:

grep -A 10 Exception app.log

Python:

grep -A 10 Traceback app.log

六、推荐的标准写法 ✅

nohup java -jar app.jar > app.log 2>&1 &

或按日期:

nohup java -jar app.jar > app_$(date +%F).log 2>&1 &

七、总结速查表

场景 日志位置
默认 nohup nohup.out
> app.log 2>&1 app.log
2> err.log err.log
不确定 lsof -p PID

如果你愿意,可以把 启动命令 + 程序类型(Java / Python / Shell) 发出来,我可以帮你精确定位错误。

0