0

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?

tripleee
  • 175,061
  • 34
  • 275
  • 318
tilch
  • 11
  • 2

1 Answers1

1

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 .

KamilCuk
  • 120,984
  • 8
  • 59
  • 111