温馨提示×

centos nohup日志如何过滤词

小樊
36
2026-09-24 10:35:16
栏目: 智能运维

在 CentOS 下使用 nohup 运行程序时,日志一般会被重定向到文件(如 nohup.out 或自定义日志文件)。过滤关键词通常有两种场景:


一、查看日志时过滤关键词(推荐)

1. 使用 grep 过滤

grep "关键词" nohup.out

忽略大小写:

grep -i "error" nohup.out

显示行号:

grep -n "timeout" nohup.out

反向过滤(不包含某词):

grep -v "DEBUG" nohup.out

多个关键词(或关系):

grep -E "error|warn|fail" nohup.out

2. 实时查看日志并过滤

tail -f nohup.out | grep "关键词"

忽略大小写实时过滤:

tail -f nohup.out | grep -i "error"

二、启动时就过滤日志(不推荐但可行)

如果你希望 nohup 启动时直接只保存某些日志:

nohup your_command 2>&1 | grep "关键词" > filter.log &

⚠️ 注意:

  • 这样会丢失未匹配的行
  • 程序退出时 grep 可能缓冲日志
  • 一般不建议生产环境这么做

三、常见实用组合

查看错误日志(实时):

tail -f nohup.out | grep -i "error\|exception"

只看某时间段日志(配合 awk):

grep "2026-01-21" nohup.out | grep "ERROR"

统计某关键词出现次数:

grep -c "timeout" nohup.out

如果你能告诉我:

  • 是 Java / Python / Shell 程序?
  • 日志是 标准输出还是文件?
  • 想 实时看 还是 事后分析?

我可以给你更精确的命令。

0 踩