温馨提示×

centos nohup日志怎么找错误

小樊
60
2026-09-09 05:27:42
栏目: 智能运维

在 CentOS 中使用 nohup 启动程序时,日志和错误通常有以下几种情况和查找方式:


一、nohup 默认日志位置

1. nohup.out

如果你是这样启动的:

nohup your_command &

默认会把 标准输出(stdout)和标准错误(stderr) 都写到当前目录的:

nohup.out

查看错误:

tail -f nohup.out
# 或查看全部
cat nohup.out

2. 指定了日志文件

如果你这样启动:

nohup your_command > app.log 2>&1 &

错误和正常输出都在:

app.log

查看:

tail -f app.log

如果只分离了错误:

nohup your_command > app.log 2> error.log &

错误在:

error.log

二、常见排查方式

1. 看进程是否还在

ps -ef | grep your_command

或

jobs -l

2. 查看退出原因

如果进程已经退出:

echo $?

(仅对刚结束的命令有效)

3. 查看系统日志(程序崩了但没写日志)

# 通用
journalctl -xe

# 老版本 CentOS
tail -f /var/log/messages

# 如果是 Java / 服务
tail -f /var/log/dmesg

4. 查看是否被 OOM 杀掉

dmesg | grep -i killed

或

grep -i oom /var/log/messages

三、推荐的正确写法(避免找不到错误)

nohup your_command > run.log 2>&1 &

或者按日期:

nohup your_command > run_$(date +%Y%m%d).log 2>&1 &

四、快速定位错误的小技巧

# 只看错误(如果有分开日志)
grep -i error error.log

# 实时看最新错误
tail -f app.log | grep -i error

如果你愿意,可以把:

  • 启动命令
  • nohup.out 或日志内容片段

发出来,我可以直接帮你判断错误原因。

0 踩