我尝试解析journald
手动生成一些错误:
for i in {1..10}; do
echo "some error #$i " | systemd-cat -p err;
done
我可以通过journalctl看到它:
journalctl -f -n 0 | grep -i 'error'
Feb 20 13:50:10 localhost [5547]: some error #1
Feb 20 13:50:10 localhost [5549]: some error #2
Feb 20 13:50:10 localhost [5551]: some error #3
Feb 20 13:50:10 localhost [5553]: some error #4
Feb 20 13:50:10 localhost [5555]: some error #5
Feb 20 13:50:10 localhost [5557]: some error #6
Feb 20 13:50:10 localhost [5559]: some error #7
Feb 20 13:50:10 localhost [5561]: some error #8
Feb 20 13:50:10 localhost [5563]: some error #9
Feb 20 13:50:10 localhost [5565]: some error #10
它可以工作,但是当我尝试在infinity while循环中使用它来在“live模式”下监听时,它不起作用:
journalctl -f -n 0 | grep -i 'error' | while read line; do echo "Error detected"; done
我怎么能这么做?谢谢!
1条答案
按热度按时间2g32fytz1#
问题是
while
循环在grep
的输出缓冲区满之前不会获得任何输入。最可移植的解决方法是用
case
语句替换grep
:**备注:**根据您的平台,您可以使用
stdbuf -oL grep -i 'error'
或bash正则表达式来代替case
。