watch dmesg, exit after first occurrence

178 Views Asked by At

I have a script which watches dmesg and kills a process after a specific log message

#!/bin/bash
while sleep 1;
do 
# dmesg -w |  grep  --max-count=1  -q 'protocol'
dmesg -w | sed '/protocol/Q'
mkdir -p /home/user/dmesg/
eval "dmesg -T > /home/user/dmesg/dmesg-`date +%d_%m_%Y-%H:%M`.log";
eval "dmesg -c";
pkill -x -9 programm
done

The Problem is that sed as well as grep only trigger after two messages. So the script will not continue after only one message.

Is there anything I am missing?

1

There are 1 best solutions below

0
KamilCuk On

You have a script that periodically executes dmesg. Instead, write a script that watches the output of dmesg.

dmesg | while IFS= read -r line; do
   case "$line" in
   *protocol*)
         echo "do something when line has protocol"
         ;;
   esac
done

Consider reading https://mywiki.wooledge.org/BashFAQ/001 .