Need help with auto-reboot

Hello everyone!

I’m a newbie at scripting, so I need some help to get this running.

I’m working with MikroTik OS 7.9 (stable), and I’ve seen many problems with LTE interfaces. I can’t find a solution (we can’t upgrade the OS version because my client doesn’t want to). Usually, the error I see is “SIM is not detected,” and I have to log in via LAN and reboot the router, or send someone to do it manually.

The idea of the patch is to use a script on the router that pings Google’s DNS, and if it fails, it will trigger a reboot.

This is what I did:

/system script add name=PingCheckReboot policy=reboot,read,write,test source=":local fail 0; :for i from=1 to=20 do={ :if ([:len [/ping 8.8.8.8 count=1 interval=300ms]] = 0) do={ :set fail (\$fail + 1) } }; :if (\$fail >= 10) do={ /log warning \"Reiniciando por pérdida de ping...\"; /system reboot}"

/system scheduler add name=RunPingCheckReboot interval=1h start-time=startup policy=reboot,read,write,test on-event="/system script run PingCheckReboot"

However, it doesn’t work. Even when I launch the script from the CLI using /system script run PingCheckReboot, the reboot prompt still needs to be confirmed.

I saw that I can’t use without-prompt=yes on my OS version, so I really don’t know how to fix it.

I would be very grateful if someone could help me!
Have a nice day :slight_smile:

FIrst thing, a forced reboot is not a very good idea (you lose log), and it should be used as last-last solution if everything else fails.

So you should try if the connection resumes after performing some “softer” actions (enable/disable the LTE interface or the USB bus, or something else depending on which device is it).

This said, if you really-really want to force a reboot, you can use (reported to be working on7.8, so likely it will work on 7.9):

:execute {/system reboot}

see:
http://forum.mikrotik.com/t/force-reboot/32053/1

:if ([/ping 8.8.8.8 count=6] = 0) do={/system reboot}

Until you test the script on the terminal, you have everytime to confirm y/n.
Only :execute “/sys reboot” can avoid that on terminal (and is :execute “/sys reboot” not :execute {/sys reboot}).


If you run the script on scheduler, confirmation are not needed.

Thank you!

I’ve been thinking about it, and I’m going to try this, using

:execute

instead of

:do

as u told.:

/system script add name=PingCheckReboot policy=reboot,test,read,write source=":local failedCount 0; :local totalCount 20; :local threshold 10; :for i from=1 to=\$totalCount do={ :if ([:len [/ping 8.8.8.8 count=1 interval=300ms]] = 0) do={ :set failedCount (\$failedCount + 1) } }; :log info (\"PingCheck: Fallos de ping: \$failedCount de \$totalCount\"); :if (\$failedCount > \$threshold)  do={ :log warning \"PingCheck: Umbral superado. Reiniciando router...\" :execute /system reboot} else={ :log info \"PingCheck: Dentro del umbral, sin reinicio.\" }"

I know rebooting should always be the last resort, but resetting the LTE interface doesn’t solve the issue, and the client still doesn’t want to let me upgrade the OS, so…

I’ll reply tomorrow to let you know if it works. Thanks a lot!

is not :execute /system reboot without quotas, is

:execute “/system reboot”


Writing script exports instead of scripts makes everything harder.

Why don’t you use winbox or terminal editor???

Also one line script is hard to fix and understand:

:local failedCount 0; :local totalCount 20; :local threshold 10; :for i from=1 to=$totalCount do={ :if ([:len [/ping 8.8.8.8 count=1 interval=300ms]] = 0) do={ :set failedCount ($failedCount + 1) } }; :log info ("PingCheck: Fallos de ping: $failedCount de $totalCount"); :if ($failedCount > $threshold)  do={ :log warning "PingCheck: Umbral superado. Reiniciando router..." :execute /system reboot} else={ :log info "PingCheck: Dentro del umbral, sin reinicio." }

on this way is more readable:

:local failedCount  0
:local totalCount  20
:local threshold   10

:for i from=1 to=$totalCount do={
    :if ([:len [/ping 8.8.8.8 count=1 interval=300ms]] = 0) do={
        :set failedCount ($failedCount + 1)
    }
}

:log info ("PingCheck: Fallos de ping: $failedCount de $totalCount")

:if ($failedCount > $threshold) do={
    :log warning "PingCheck: Umbral superado. Reiniciando router..."
    :execute "/system reboot"
} else={
    :log info "PingCheck: Dentro del umbral, sin reinicio."
}

also as export:

/system script
add dont-require-permissions=no name=PingCheckReboot owner=admin policy=reboot,read,write,test source="\r\
    \n:local failedCount  0\r\
    \n:local totalCount  20\r\
    \n:local threshold   10\r\
    \n\r\
    \n:for i from=1 to=\$totalCount do={\r\
    \n    :if ([:len [/ping 8.8.8.8 count=1 interval=300ms]] = 0) do={\r\
    \n        :set failedCount (\$failedCount + 1)\r\
    \n    }\r\
    \n}\r\
    \n\r\
    \n:log info (\"PingCheck: Fallos de ping: \$failedCount de \$totalCount\")\r\
    \n\r\
    \n:if (\$failedCount > \$threshold) do={\r\
    \n    :log warning \"PingCheck: Umbral superado. Reiniciando router...\"\r\
    \n    :execute \"/system reboot\"\r\
    \n} else={\r\
    \n    :log info \"PingCheck: Dentro del umbral, sin reinicio.\"\r\
    \n}\r\
    \n"

But the most important question is:
why if there is watchdog for this specific thing,
you prefer to waste time making scripts?


https://help.mikrotik.com/docs/spaces/ROS/pages/8978694/Watchdog

If 8.8.8.8 do not respond for 6 times on 60 seconds, the device reboot (1 ping each 10 seconds is the default)
but you can change the interval to 6s, so it ping 8.8.8.8 each second for 6 seconds, so if do not reply, the unit reboot.

Although what @jaclaz wrote is correct. Maybe trying a USB/PCIe reset first is faster and less shocking for the RouterBOARD.