Background
Currently, many universities implement a one-device policy in dormitories, where each person must have their own internet connection, and only two devices are allowed to connect. This is said to be for better network management and to trace individual usage. However, it is quite absurd, as the internet fees are high, and usage is low, making personal connections quite cumbersome. Nevertheless, there are always workarounds; a common solution is to flash third-party firmware such as OpenWrt, Merlin, or Pandora, with OpenWrt being the most widely used open-source option.
Our dormitory’s old second-hand router has OpenWrt flashed on it. While it works well, it frequently loses internet connection, requiring me to crawl out of my warm bed to manually restart it, which is quite troublesome.
OpenWrt supports crontab, a task management system on Linux that can execute scheduled tasks. With it, we can achieve “automatically check network connectivity at regular intervals and decide whether to restart the router”.
Implementation
Here is the official wiki for cron on OpenWrt:
https://openwrt.org/docs/guide-user/base-system/cron
According to the documentation, there are two ways to configure cron: one is to access the console at 192.168.1.1, and the other is to enter the backend via SSH. We will choose SSH, as we will also write a shell script later.
After entering the backend, we will first write a checkNet.sh script to check network connectivity and restart if it is not connected. The ping command comes to mind. The script will be placed in the root directory.
#! /bin/bash# Check network connectivityping -c 1 baidu.com > /dev/null 2>&1if [ $? -ne 0 ];then echo Network connectivity check failed, restarting rebootfi # Restart network:# /etc/init.d/network restart#
It looks like this:
After writing, you can test it:
sh checkNet.sh
If there is no output, then the current network is normal.
Next, enter the crontab command to see what commands it supports.
crontab
Here we choose “-e“, which takes us to the editing interface.
crontab -e
If you are unsure how to write the cron command or if you are uncertain about what you have written, you can refer to this website:
https://tool.lu/crontab/
We will set it to execute the script every 10 minutes, which corresponds to the following cron:
*/10 * * * *
Thus, our final crontab command will be:
*/10 * * * * sh checkNet.sh
Just write it into the above crontab -e section. You can see that it has indeed been written in.
Of course, you can also add it directly in the 192.168.1.1 console, and it will show the same:
Alright, that’s all done~~