在 Linux 下使用 nohup 运行程序时,日志通常会重定向到 nohup.out 或指定文件。如果你想过滤日志中的关键词,常见做法如下:
tail -f + greptail -f nohup.out | grep "关键词"
只显示包含“关键词”的行。
tail -f nohup.out | grep -i "error"
tail -f nohup.out | grep -E "error|warn|exception"
tail -f nohup.out | grep -v "DEBUG"
不显示包含 DEBUG 的行。
grep "关键词" nohup.out
grep -n "error" nohup.out
grep -c "error" nohup.out
grep --color=auto "error" nohup.out
如果你希望 nohup 运行时就只保存过滤后的日志:
nohup your_command | grep "关键词" > filter.log 2>&1 &
⚠️ 注意:这样会丢失未匹配的行,一般不推荐。
less 查看大日志less nohup.out
在 less 中:
/关键词 搜索n 下一个N 上一个如果你能告诉我:
我可以给你更精确的命令。