在 CentOS 下使用 nohup 运行程序时,日志一般会被重定向到文件(如 nohup.out 或自定义日志文件)。过滤关键词通常有两种场景:
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
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
如果你能告诉我:
我可以给你更精确的命令。