在 CentOS 下使用 nohup 启动程序时,错误日志主要看 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 &
此时:
tail -f app.log
grep -i error app.log
grep -i exception app.log
nohup java -jar app.jar > out.log 2> err.log &
错误日志在:
err.log
查看:
tail -f err.log
ps -ef | grep app.jar
或:
jps -l
lsof -p PID
重点看:
cwd:当前目录(nohup.out 可能在这里)1w / 2w:stdout / stderr 输出文件ps -ef | grep app.jar
没有进程说明启动失败,错误可能在:
ls -l nohup.out
chmod 755 app.jar
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) 发出来,我可以帮你精确定位错误。