Conditional reload

It is useful to restart the router from time to time, for example at 4:00 am. I can use Scheduled Tasks for this. But I need to consider one condition.
If a certain device is connected to the WIFI network, then the router cannot be restarted.
How can this be done?

You can write a short script that uses iwinfo wlanX assoclist | grep MAC of the device as variable and restarts the router if the output of it is empty. You can then use crontab to schedule running the script at 4 a.m. each day.

2 Likes

Thanks a lot. This works from the command line. But I don't know how to create the script correctly.
Created a file (only the /ruut directory is available in ssh). How to test it?
Need to install bash ?
Is there a manual for creating scripts on OpenWrt?

If we are talking about only one device, you could try this:

cat << "EOF" >> /etc/crontabs/root
0 4 * * 0 if ! cat /proc/net/arp | grep -q aa:bb:cc:dd:ee:ff; then sleep 70 && touch /etc/banner && reboot; fi
EOF
/etc/init.d/cron restart

Edit the MAC address and time/day of the week accordingly.

2 Likes

Excellent. Is it for command line?
And how then to edit or delete this line?

You can enter this in the command line. To delete it, simply use crontab -e to open the cronjob file for root, go to the line that has your condition in it, press dd to delete it and use :wq to save the cronjob file again. Afterwards you can check it with crontab -l to see whether the deletion was successful.

However, I wouldn't use arp or dhcp.leases to check for the MAC. This is because arp usually has all MAC addresses that have gotten an IP address from your router since last reboot, regardless of current connection status. cat /tmp/dhcp.leases on the other hand saves IP addresses and the corresponding MAC for the leasetime you set. So your device might already have disconnected from wifi but the lease is still held until the leasetime is met (e.g. 60 minutes).

As far as I know the available options, only the assoclist command gives you the correct information on current, upheld connections to the respective wifi. Pairing the arp command with a ping to the device, to check whether it is reachable, might also prove inconclusive because the device could be "unpingable" in general.

So I recommend using iwinfo wlanX assoclist instead of the arp command with grep.

2 Likes

Thanks to all! It's woking!

Watchcat package.

3 Likes

Interesting. But the watchcat cannot check the condition I need - whether a certain device is connected.

It can ping this host and use that as a connectivity condition.

Thanks, I'll take a look. I will need a fixed IP for this device. If it can NOT reboot in the presence of a ping. it was meant to be just the opposite case.

If the host responds to ping, it will not be restarted.

I realize that @Nihilokrat did provide a good answer to your question, but I want to point out this message from a couple months ago. Thanks.

2 Likes

I really see that over time, the download speed drops and clients in the poor coverage area (for example, Wi-Fi bulbs) lose contact with the router. After reboot everything is restored..

Wi-Fi lightbulbs? Oh my - if you have the time and energy to pull together some diagnostic information, that would be a really interesting trouble report. Here's what I would want to know:

Static info:

  • Router brand and model
  • OpenWrt firmware version
  • Lightbulb brand and model
  • Connected at 2.4GHz? 5 GHz?
  • Anything else you think might be relevant (distance from router?)

Time sequence:

  • How do you measure "download speeds"? What test do you use? What kind of computer/device?
  • What's the "normal speed" just after everything reboots? How long does it take to change? What does it drop to?
  • What are the light bulbs doing in this time? Are they working normally? Or do they lose contact?
  • Does rebooting the computer (that you used for the speed test) make a difference in its speed?
  • Does unplugging (totally powering down the lightbulb) make a difference?
  • Is this repeatable?

Thanks!

1 Like

OK. Write in this topic?

It would be better to start a new topic, maybe with a subject line of "Speed drops after a week" (or whatever time period it takes...)

How can I make sure that the router reboots at the right time? What log file?

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