温馨提示×

怎样搜索nohup日志内容

小樊
44
2026-08-19 10:43:06
栏目: 编程语言

在 Linux 中,nohup 命令通常会把输出重定向到 nohup.out 文件(除非你手动指定了输出文件)。搜索 nohup 日志内容,常见做法如下:


一、找到 nohup 日志文件

1️⃣ 默认情况

ls -l nohup.out

一般位于你启动 nohup 命令的当前目录。

2️⃣ 手动指定了日志文件

例如:

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

那么日志文件就是 app.log


二、常用搜索方式

✅ 1. 使用 grep(最常用)

搜索关键字

grep "关键字" nohup.out

忽略大小写

grep -i "error" nohup.out

显示行号

grep -n "Exception" nohup.out

显示匹配行的上下文

grep -C 5 "error" nohup.out   # 前后各5行

实时搜索(日志还在写入)

tail -f nohup.out | grep "error"

✅ 2. 使用 less(适合大文件)

less nohup.out

常用操作:

  • /关键字:向下搜索
  • ?关键字:向上搜索
  • n:下一个匹配
  • N:上一个匹配
  • q:退出

✅ 3. 使用 tail 查看最新日志

tail -n 100 nohup.out

实时查看:

tail -f nohup.out

✅ 4. 使用 cat + grep(不推荐大文件)

cat nohup.out | grep "error"

三、按时间范围搜索(进阶)

1️⃣ 搜索最近 10 分钟写入的内容

find . -name nohup.out -mmin -10 -exec tail -n 100 {} \;

2️⃣ 结合时间字段(日志中有时间时)

grep "2026-01-21 14:" nohup.out

四、常见问题排查

❓ 找不到 nohup.out

  • 是否在别的目录启动?
  • 是否重定向到了其他文件?
  • 是否被清理过?

查找可能的日志文件:

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

五、推荐组合(实战)

查看实时错误日志

tail -f nohup.out | grep -i error

查找异常堆栈

grep -n -A 10 "Exception" nohup.out

如果你愿意,可以把:

  • 启动命令
  • 日志文件路径
  • 想搜索的具体内容(如报错信息)

发给我,我可以帮你写一条精准的搜索命令

0