How to reboot router when a string appears in the log

Hello

Like the title says, due to my certain requirement I want to reboot my router when a certain line of string appears in the log. For example..

Network device 'wlan0' link is up

Or

DNS service limited to local subnets

It could be any line in the system log.
It must match line if it shows in the log then execute reboot immediately.

Can it be done?

Yes, you should create script like:
logread -f | while read -r line
do
if...
fi
done

1 Like

Thanks .. but where do I need to put this script ?

The simplest option is in /etc/rc.local
More complicated option is to create init script: https://openwrt.org/docs/techref/initscripts

Can it be done? Yes. Should it be done? Probably not. Most operational problems can be solved without rebooting. You also risk your script causing a boot loop and making the router completely inaccessible.

2 Likes

Phylosoph in thread?

Yes I understand what ur saying.. but the issue I'm facing can only be solved by reboot. It happens randomly every 2 - 3 days.
If u search about it.. u will know.

phy0 rt2x00queue_write_tx_frame error – dropping frame due to full tx queue 2

So I just want to make a script to detect this line and reboot. I can't update my router to the latest version due to usb not working issue. So this is the only solution I could think of.

I was wondering could someone please make the script for me like @ulmwind mentioned since I'm not quite familiar with this language if u don't mind. I would really appreciate it..

What is the problem?

if [ "$line" = "your string" ]; then
 reboot
fi

Thanks
i have pasted this in /etc/rc.local. It should work i believe.

That's only the comparison part. You need to put it inside the do loop that @ulmwind posted to have it wait for new lines to be posted to the log and compare each one.

The shell in OpenWrt is ash; it is very similar to bash but missing some of the more advanced features. Look for a reference on ash or bash to learn the language.

Yes, as @mk24 have mentioned, it is only comparison part.

Make following test:
create empty file /tmp/file.txt, and add following strings to script

tail -f /tmp/file.txt | while read -r line
do
 if [ "$line" = "abc" ]; then
  reboot
 fi
done

After that login by ssh, and add corresponding line to file:
echo abc >> /tmp/file.txt
Router should reboot. If not, write here.

No reboot, abc is written to file.txt.

If i got it correctly, then i needed to put following strings into /etc/rc.local before exit 0 right?

tail -f /tmp/file.txt | while read -r line
do
 if [ "$line" = "abc" ]; then
  reboot
 fi
done

Save this script, e.g. /root/phy0-watch.sh. Run it with /bin/sh /root/phy0-watch.sh

#!/bin/sh
sig="rt2x00queue_write_tx_frame error"
logread -f | while read line; do
    if [ "${line#*$sig*}" != "$line" ]; then
        echo reboot # remove the "echo" if it works
    fi
done

Then you can test it by running in another terminal

logger phy0 rt2x00queue_write_tx_frame error – dropping frame due to full tx queue 2

Install a package named pservice

opkg update
opkg install pservice

Edit /etc/config/pservice to have a section like the following

config pservice
	option name 'demo0'
	option command /bin/sh
	list args '/root/phy0-watch.sh`

Enable and start pservice

/etc/init.d/pservice enable
/etc/init.d/pservice restart
2 Likes

Thanks..

I tested the above code and it works with echo reboot.

But when i tried to install pservice package, i'm receiving this error, i did used opkg update before.

root@OpenWrt:~# opkg install pservice
Unknown package 'pservice'.
Collected errors:
 * opkg_install_cmd: Cannot install package pservice.

Is there an alternative to pservice?

I'm using 18.06.1 build.

pservice is a late addition to the packages feeds. Likely it's not available in the 18.06 version. But itself is a simple init script.

Then config and start like previously said

I did as mentioned, then next error when sending this command 'pservice restart'

root@OpenWrt:~# /etc/init.d/pservice restart
/etc/rc.common: eval: line 1: uci_load_validate: not found
/etc/rc.common: line 1: uci_load_validate: not found

is this error normal?

Grr.. that uci_load_validate is also only available since 19.07.. A previous version of pservce.init does not depend on it,

Thanks... all went ok at this point.
But after reboot my script is not running. I waited for 10 minutes after initialization.

It only works when the script runs manually by this command

/bin/sh /root/phy0-watch.sh

then running test with command

logger phy0 rt2x00queue_write_tx_frame error – dropping frame due to full tx queue 2

The script should run automatically on boot. What am i doing wrong?

Sorry, it looks like quoting in previous answer was incorrect. Note that backquote in the following line, it should be single quote "'".

If it still does not work. Please share output the following commands

ls -l /etc/rc.d | grep -i pservice
cat /etc/config/pservice