/sys usage
Note that a much easier way to find devices in /sys is to ask udev:
for DIR in $(udevadm trigger -v -n -s usb -a idVendor=0bda -a idProduct=8153)
for DIR in $(udevadm trigger -v -n -s usb -p ID_VENDOR_ID=0bda -p ID_MODEL_ID=8153)
Periodic actions
To do periodic actions, one normally uses cron. If you want to grep dmesg for your message once a minute, then just add a cron job that runs once a minute and greps dmesg for your message.
* * * * * if dmesg | tail -100 | grep -qs "r8152.*Tx status -71"; then /etc/kick-realtek.sh; fi
(The five asterisks are part of the crontab format and indicate the schedule time & date.)
One problem with that simple approach is it'll react to all such messages, even old ones logged before the last reset. You could avoid this by writing a marker line to dmesg (or even using an existing kernel message like "USB device detected" as the marker):
#!/usr/bin/env bash
if dmesg | tail -100 | awk '/r8152.*Tx status -71/ {x=1} /KICKED/ {x=0} END {exit(!x)}'; then
/etc/kick-realtek.sh;
echo "KICKED" > /dev/kmsg;
fi
Alternatively you could read and remember the last timestamp of such an error message, but that's much too complex in comparison.
Live monitoring
An even better option would be to use a program that continuously receives the log messages instead of having to do periodic checks. For example:
dmesg --follow | ...
journalctl --dmesg --lines=10000 --follow | ...
tail -n 10000 -f /var/log/kern.log | ...
Regardless of which one is used, pipe its output into a loop that checks each incoming line. Run the resulting script in background, ideally you could even turn it into a system service (/etc/systemd/system/*.service):
#!/usr/bin/env bash
... | while read -r line; do
if [[ $line == *r8152*Tx\ status\ -71* ]]; then
/etc/kick-realtek.sh
fi
done
(You could use if echo "$line" | grep -qs ...; then or similar, it doesn't matter.)
Finally, many server systems already have a program that continuously receives all log messages, such as rsyslog or syslog-ng. (These programs are how dmesg gets copied to /var/log in the first place.) They can often filter the log messages and run programs: