DanielS
DanielS

Reputation: 53

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?

Upvotes: 3

Views: 168

Answers (2)

DanielS
DanielS

Reputation: 53

Thank you for the great start miken32!

I've been able to take your sample and tweak it to work for my application with the following:

#!/bin/bash
pin=0
gpio mode $pin out
gpio write $pin 0
tailf /var/log/pihole.log | while read INPUT
do
   if [[ "$INPUT" == *"/etc/pihole/gravity.list"* ]]; then
       gpio write $pin 1
       sleep 1
       gpio write $pin 0
    fi
done

Thanks again for your help!

Upvotes: 1

miken32
miken32

Reputation: 42714

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.

Upvotes: 2

Related Questions