First of all, let’s fix one issue in the code. I was inattentive and just copied timeout parameter from your code. There is no such parameter in ping command, it should be replaced by interval.
You need a scheduler anyway, doesn’t matter if you use your on mine code. Both of them will run only one time, when you execute them. So, scheduler is required to launch the script with some interval. Because the script execution time is floating (it could be little more than 10 seconds, when everything is fine and interface is not restarted, or it could be more than 15 seconds, when interface is restarted), you need about 20 seconds interval for your scheduler task. Or may be reduce it, but in such case it’s required to check in scheduler’s code, if a script is already running before executing it, to avoid multiple instances.
And one more very important thing. Because you are using lte interface, its startup time could be quite long. I don’t know, how fast is yours, but my lte interface startup time is about 15 seconds. In such case, if your script restarts lte interface after unsuccessful ping, it will then do pinging, using a not yet running interface. But even if the interface startup time is less, the script will anyway start pinging, when interface is not yet running. This will lead to infinite interface restarting condition, and your interface will never go up!
To solve this, the script should check, if the lte interface is running prior to pinging anything.
So, you need to add the following to your code:
:local state [/interface/lte/get $interfaceName running]
:if ($state = true) do={
#ping command here
}
And your code will look like this:
:local targetIP "8.8.8.8"
:local interfaceName "lte1"
:local state [/interface/lte/get $interfaceName running]
:if ($state = true) do={
:if ([/ping $targetIP interface=$interfaceName count=10 interval=1s] = 0) do={
/log warning ("[LTE Watchdog] No internet for 10s. Rebooting interface $interfaceName")
/interface lte disable $interfaceName
:delay 5s
/interface lte enable $interfaceName
}
}
So, I would suggest 2 options for you:
- In addition to the code above, you need to add a scheduler task, that will periodically execute a script.
The code for scheduler will be like that:
:local sName "YourScriptName"
:local isRunning [/system script job print count where script=$sName]
:if ($isRunning = 0) do={
/system script run $sName
}
With this code you can set any interval for scheduler within 10-20 seconds, it won’t allow more than 1 script instance to run. I would recommend 15 seconds. An of course set start-time parameter of your scheduler task to startup.
- Regarding main infinite loop, I mean this:
:local targetIP "8.8.8.8"
:local interfaceName "lte1"
:do {
:local state [/interface/lte/get $interfaceName running]
:if ($state = true) do={
:if ([/ping $targetIP interface=$interfaceName count=10 interval=1s] = 0) do={
/log warning ("[LTE Watchdog] No internet for 10s. Rebooting interface $interfaceName")
/interface lte disable $interfaceName
:delay 5s
/interface lte enable $interfaceName
}
}
:delay 100ms
} while=(true)
Here I’ve placed the working part of the code into infinite loop, so this script will run on its own and doesn’t need to be executed by scheduler every 15 seconds. It should be executed by scheduler only one time on router startup. You can use the same scheduler code as in option 1 for that, with start-time parameter set to startup and interval set to 0.
But for reliability I do recommend to use a scheduler as a watchdog, so it will periodically check, if a script is running. Even if your script is ideal, it could fail for many reasons, including RouterOS bugs. So, just set scheduler task interval to let’s say 30 seconds or 1 minute, or choose some other value you like. This will be the interval for checking if the script is running, and if for some reason it’s not running, it will be just started.
Also, I usually add something like that to the very beginning of the script:
:while ([/system/resource/get uptime] < 1m) do={
:delay 10s
}
This will postpone script execution for the first minute after router startup. There is no any sense to check interface immediately after router startup, when interfaces are not yet started. You may adjust this value for your needs.