What's new
  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Need a script that auto reboot if internet is down

Hi,

Please accept my apologies, I've tried reading the readme/included manual but I don't think I fully understand it. I hate it when people do their best to describe something as detailed and clearly as possible, but then I come along and just don't get it. :(

I have four questions about this neat script:

1.) When using "force" or "forcesmall", does it always REBOOT the router even when you specify "wan"?
Example: "ChkWAN wan ping=8.8.8.8 nowait forcesmall" would ignore "wan" and reboot the router anyway because you use "forcesmall"? (The description for "force" and "forcesmall" both explicitly mention it will *reboot* the router if it fails, hence my question)

2.) It is possible to use multiple hosts to ping. Is it also possible to use two curl hosts? Eg: "forcesmall" seems to connect to github whilst a 10mb file appears to connect to an OVH server with "force". If GitHub has an interruption when ChkWAN runs with the small file, this results in a failure and will, potentially needlessly, restart the WAN interface or reboot the whole thing depending on your configuration. Ideally, you have it check another host to confirm. Is that doable or is it better to solely rely on ping when this is of concern?

3.) (answered myself, but keeping in case someone wonders)
Double checking: usually the way I setup a cronjob is a script in jffs that's triggered by "cru" every 5 to 15 minutes and then it just does its thing and dies until the next round of running. This script however seems to have something on board to create the cronjob for you. Am I correct that it is not necessary to use this "cron" option? (Curious because of the "status" function)

If so, is this the correct way to set it up to ping 3 hosts and if that fails restart the WAN interface (never reboot)?
/jffs/scripts/ChkWAN.sh wan ping=208.67.220.220,8.8.8.8,1.1.1.1 nowait

-edit- Actually by the looks of it when doing this as a cron job every 10 minutes, every 10 minutes a new process will spawn that starts re-checking the connection every 30 seconds? It doesn't terminate after successful run?
-edit2- Sorry, evaluating the code it seems that it checks if another instance isn't already running.

-edit3-So this indeed seems to be working. So I think I've answered my own question here: no you do not need to use the "cron" option of the script. Also: it seems to detect when there's an active cronjob for ChkWAN so it auto-terminates after run. :)


4.) The cron explanation section mentions setting up various cronjobs where it does 2x restart WAN and 1x REBOOT. However, the cronjobs as I read them do not really seem to be mutually exclusive. So if the 2 attempts in cron to restart the WAN were running just fine with no failures, but then internet goes down for 2 minutes at exactly the time the REBOOT cronjob runs: it'll trigger a full router reboot? Its not like the WAN check being successful stops the REBOOT cron from running? I must be overlooking something? :)

Thank you for your help and patience! :)


-edit-
Also, I know this is may seem a bit out of place after asking so many questions on the command syntaxes and all that, but there is a small mistake in the examples for cru that I'd like to point out in case it causes issues:
# Set up a static cron schedule every 10 minutes 2 times WAN Restart 3rd REBOOT
# cru a Restart_WAN 00,10,30,40 * * * * /jffs/scripts/ChkWAN.sh wan force no wait
# cru a Reboot_WAN 20,50 * * * * /jffs/scripts/ChkWAN.sh reboot force nowait
# but /jffs/scripts/ChkWAN_Reset_CRON.sh can change after very successful WAN UP check (Syslog monitor is better!!!?)
# cru a Restart_WAN 28,38,58,8 * * * * /jffs/scripts/ChkWAN.sh wan force nowait
# cru a Reboot_WAN 48,18 * * * * /jffs/scripts/ChkWAN.sh reboot force nowait

Copy/pasting any of these examples will all result in issues, as cru demands the cron/command portion is put in quotes - otherwise really weird things can happen and you get utterly malformed cronjobs running (might even start running the entire content of /jffs/scripts). Example:
cru a Restart_WAN 00,10,30,40 * * * * /jffs/scripts/ChkWAN.sh wan force no wait
Should be: cru a Restart_WAN "00,10,30,40 * * * * /jffs/scripts/ChkWAN.sh wan force no wait"


-edit2-
Also, in case anyone finds it useful. So I had setup that cronjob to run every 5 minutes, but I figured I don't want this to actually run when the uptime of the device is less than 5 minutes. Give it a moment to breathe and negotiate stuff with the ISP and all that. So I've added to the top of the script (like right below the version line (with a white line in between)):
Code:
# Check uptime and halt execution if uptime is less than 5 minutes
uptime_seconds=$(cut -d. -f1 /proc/uptime)
if [ "$uptime_seconds" -lt 300 ]; then
  logger -t ChkWAN "Skipped run: uptime ${uptime_seconds}s (< 300s threshold)"
  exit 0
fi

This means when the cronjob triggers, but the router hasn't been up for more than 5 minutes yet: stop execution and write a log entry saying as much. Of course you can change this to a lower or higher treshold, for me 5 minutes is fine (means at worst if the connection didn't come up its 9 minutes and 59 seconds of no action taken by the script directly after a reboot (because when it tries to run at exactly 299 seconds (4m59s) uptime: it won't run, causing the 5 minute wait until next cronjob runs == 9m59 since coming up before script runs.)). Figured I'd share in case people find this a useful modification.
 
Last edited:

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Back
Top