How to reboot router when a string appears in the log

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

OK, it is not good example, because file is deleted after reboot from /tmp Please, create corresponding file in /etc/config

Thanks a lot.. it works at last.

Here is the output just in case

root@OpenWrt:~# ls -l /etc/rc.d | grep -i pservice
lrwxrwxrwx    1 root     root            18 Feb 20 17:44 S99pservice -> ../init.d/pservice


root@OpenWrt:~# cat /etc/config/pservice
config pservice
        option name 'demo0'
        option command /bin/sh
        list args '/root/phy0-watch.sh'
1 Like

I did, but something was not right. The script was not running at reboot. I must've rebooted many times already just to check that, maybe my script was not correct somehow. Still thanks a lot for your help..

If your problem is solved, please consider marking this topic as [Solved]. See How to mark a topic as [Solved] for a short how-to.

OK, I've checked:

tail -f /etc/config/file.txt | while read -r line
do
 echo $line >> /etc/config/file.out
 if [ "$line" = "abc" ]; then
  echo "LINE" >> /etc/config/file.out
 fi
done

root@OpenWrt:/etc/config# cat file.out
12u
23i
root@OpenWrt:/etc/config# echo abc >> file.txt
root@OpenWrt:/etc/config# cat file.out
12u
23i
abc
LINE
root@OpenWrt:/etc/config#

The problem is that command 'reboot' doesn't work, because /etc/rc.local doesn't return 0, to my mind. Try to create another script, and run it from /etc/rc.local:

/etc/config/script.sh &

exit 0
1 Like

Well, guess what... It works... !
I wish i could select multiple answers as solution. Thanks a million

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.