Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tail a constantly updated logfile and perform an action when a string is found

I have an odroid-c1+ that I would like to use as a pi-hole server (basically dns blackhole for ad's)

I would like to trigger an led to blink when a string is found in the logfile.

I also have wiringpi installed and working, the example blink.sh works as expected as follows:

PIN=0

gpio mode $PIN out

while true; do
  gpio write $PIN 1
  sleep 0.5
  gpio write $PIN 0
  sleep 0.5
done

How would one go about adding the tailf trigger to this sample?

like image 773
DanielS Avatar asked Dec 13 '25 02:12

DanielS


1 Answers

Untested, but I believe you can feed the output from tail into your while loop:

#!/bin/bash
pin=0
gpio mode $pin out
tail -f logfile | while read entry
do
   if [ "$entry" = "string" ]; then
       gpio write $pin 1
       sleep 0.5
       gpio write $pin 0
       sleep 0.5
    fi
done

Uppercase variable names are traditionally reserved for the shell's use.

like image 91
miken32 Avatar answered Dec 15 '25 19:12

miken32



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!