MikroTik Script: Router Rebooted Script

MikroTik Script: Router Rebooted Script

This is a useful little RouterOS script that will email you a nice report when your router reboots. The emailed report contains recent critical log events that may point you to the cause for the reboot. ie “router was rebooted without proper shutdown” or “out of memory condition was detected”.

The Router Reboot Script

:delay 1

:local reportBody ""

:local deviceName [/system identity get name]
:local deviceDate [/system clock get date]
:local deviceTime [/system clock get time]
:local hwModel [/system routerboard get model]
:local rosVersion [/system package get system version]
:local currentFirmware [/system routerboard get current-firmware]
:local upgradeFirmware [/system routerboard get upgrade-firmware]


:set reportBody ($reportBody . "Router Reboot Report for $deviceName\n")
:set reportBody ($reportBody . "Report generated on $deviceDate at $deviceTime\n\n")

:set reportBody ($reportBody . "Hardware Model: $hwModel\n")
:set reportBody ($reportBody . "RouterOS Version: $rosVersion\n")
:set reportBody ($reportBody . "Current Firmware: $currentFirmware\n")
:set reportBody ($reportBody . "Upgrade Firmware: $upgradeFirmware")
if ( $currentFirmware < $upgradeFirmware) do={
:set reportBody ($reportBody . "NOTE: You should upgrade the RouterBOARD firmware!\n")
}

:set reportBody ($reportBody . "\n\n=== Critical Log Events ===\n" )

:local x
:local ts
:local msg
foreach i in=([/log find where topics~"critical"]) do={
:set $ts [/log get $i time]
:set $msg [/log get $i message]
:set $reportBody ($reportBody  . $ts . " " . $msg . "\n" )
}

:set reportBody ($reportBody . "\n=== end of report ===\n")

/tool e-mail send subject="[$deviceName] Router Reboot Report" to="yourname@example.com" body=$reportBody

Install the script

Using Winbox, copy and paste the above script into System | Scripts on your MikroTik Router.  The script should be named router-reboot-report.  Here is a screenshot of the installed routeros script.

Setup script to run on Startup

After you have added the script, navigate to System | Scheduler and create a new schedule to run on startup. Here is a screenshot of what the schedule looks like.

Mikrotik Winbox Scheduler Screenshot

We want the script to run every time the router reboots so from the Start Time dropdown choose “startup

In the On Event section of the schedule paste the following commands.

:delay 30
/system script run router-reboot-report

The script name should exactly match the name of the script you added.  The 30 second delay gives the router a chance to get connectivity after reboot.  If the email fails to send then you may need to increase the delay.

Configure Logging

For this script to function properly you must configure persistent logging on your MikoTik Router.  If your logs are not persistent across reboots you may miss some of the critical log events that tell the story of why it reboot in the first place.  To configure persistent logging please see the Logging page on the mikrotik wiki.

Example Email Report

This is what the report looks like, you will receive this email each time your router reboots.  As you can see in the example my router recently rebooted with an out of memory condition.  The report also provides the current version RouterOS packager version as well as the firmware version information.

This script can help troubleshoot random reboots and unexpected crashes. I hope you find this MikroTik Script useful!

Leave a comment below or checkout my other MikroTik Tutorials.

NetScout LinkRunner G2

LinkRunner G2 is the ultimate network cable test tool


CAT5 Cable Tester, Measure Cable Length,
PoE Voltage, Network Connectivity, Switch Port ID
Optional Wireless & Fiber Optics Modules
Check Price on Amazon

12 Replies to “MikroTik Script: Router Rebooted Script”

  1. This only needs critical logging enabled to disk correct? Do you recommend them all be stored on flash? Thanks – this is great work.

  2. Greeting from Germany, very good script. Is it possible to send after reboot the mikrotik also to send via email the new cloud IP address if it changed? best regards db4rz I am a german radio amateur DB4RZ

  3. Thanks very much for this,
    Just set it up per your very clear instructions and all is working well.
    I have not configured full logging yet, but even without that I can see when a reboot happens.
    Best Regards,
    Daniel in Oregon

  4. Thanks again JC,
    This script is working flawlessly for me.
    One follow-up question:
    How would I modify the script to write to a local file instead of sending an email?
    I assume it is easy, just don’t know the syntax.
    Best regards,
    DB

  5. Hi again,
    For what it’s worth:
    I found that the latest Mikrotik RouterOS upgrade (7.1.1) seems to have
    broken this very useful script. I’ve tested it on both my hEX and hAP^3 units.
    After downgrading back to 6.49.2, everything is good again.
    Regards,
    DB

  6. I just realized that you covered logging to disk in a previous tutorial.
    “How to configure persistent logging”
    That very likely answers my previous question and I will give it a go.
    Thanks again,
    DB

  7. Still working fine on RouterOS Version: 7.5.

    Though changed
    :local rosVersion [/system package get system version]
    for
    :local rosVersion [/system package get routeros version]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.