Skip to content
Back to Blog
MikroTik

Scheduler and Scripts in MikroTik: Automating Tasks

Write RouterOS scripts and schedule them with the Scheduler tool to automate repetitive tasks like backups, reports, and interface restarts.

Aug 2026
11 min read

RouterOS Scripting and the Scheduler

RouterOS has a built-in scripting language and a scheduler that lets you automate repetitive tasks. From simple daily reboots to complex network automation, scripts and the scheduler are essential tools for any MikroTik administrator.

RouterOS Scripting Basics

RouterOS scripts are sequences of CLI commands. You store them under /system script and run them manually or on a schedule.

Creating a Script

TEXT
/system script add name=my-first-script source={
  /log info message="Hello from script!"
}

Running a Script Manually

TEXT
/system script run my-first-script

Viewing All Scripts

TEXT
/system script print

Script Syntax Basics

RouterOS uses its own scripting syntax:

Variables

TEXT
:local myVar "Hello"
:log info $myVar

Conditional Logic

TEXT
:if ([:ping 8.8.8.8 count=1] = 0) do={
  :log warning "Google DNS unreachable"
} else={
  :log info "Google DNS reachable"
}

Loops

TEXT
:for i from=1 to=5 do={
  :log info ("Iteration: " . $i)
}

String Concatenation

TEXT
:local ip "192.168.1.1"
:log info ("Gateway is: " . $ip)

Practical Script Examples

Example 1: Daily Backup

TEXT
/system script add name=daily-backup source={
  /system backup save name=("backup-" . [:pick [/system clock get date] 0 10])
  :log info "Backup completed"
}

This creates a backup file named with today's date.

Example 2: Clear DNS Cache

TEXT
/system script add name=clear-dns source={
  /ip dns cache flush
  :log info "DNS cache cleared"
}

Example 3: Restart a Specific Interface

TEXT
/system script add name=restart-ether1 source={
  /interface disable ether1
  :delay 3s
  /interface enable ether1
  :log info "ether1 restarted"
}

Example 4: Block an IP Address

TEXT
/system script add name=block-suspicious source={
  /ip firewall address-list add list=blocked address=10.0.0.99 timeout=1h
  :log warning "Blocked 10.0.0.99 for 1 hour"
}

The Scheduler

The scheduler runs scripts at defined times or intervals automatically.

TEXT
/system scheduler

Adding a Scheduled Task

TEXT
/system scheduler add name=daily-reboot interval=1d start-time=03:00:00   on-event="/system reboot" comment="Reboot at 3am daily"

Parameters:

  • name — identifier for the task
  • interval — how often to run (1d = daily, 1h = hourly, 00:05:00 = every 5 minutes)
  • start-time — time of day to first run
  • start-date — optional specific start date
  • on-event — the command or script to run

Schedule a Stored Script

TEXT
/system scheduler add name=run-backup interval=1d start-time=02:00:00   on-event="/system script run daily-backup"

Schedule Every 5 Minutes

TEXT
/system scheduler add name=check-dns interval=00:05:00   on-event="/system script run clear-dns"

Schedule a One-Time Task

TEXT
/system scheduler add name=one-time-task start-date=2026/07/01 start-time=10:00:00   interval=0 on-event="/system script run my-first-script"

Setting interval=0 means it runs once and stops.

Viewing the Schedule

TEXT
/system scheduler print

Example output:

TEXT
# NAME          START-DATE   START-TIME INTERVAL  ON-EVENT
0 daily-reboot  jan/01/1970  03:00:00   1d        /system reboot
1 run-backup    jan/01/1970  02:00:00   1d        /system script run daily-backup

Useful Built-in Functions

Get Current Time

TEXT
:local now [/system clock get time]
:log info ("Current time: " . $now)

Get Interface Status

TEXT
:local status [/interface get ether1 running]
:if ($status = true) do={ :log info "ether1 is up" }

Find and Modify Objects

TEXT
/ip route set [find dst-address="0.0.0.0/0" comment="Primary WAN"] distance=10

The [find ...] construct lets you select objects by property and pass them to another command.

Debugging Scripts

Use :put to print values during testing in the terminal:

TEXT
:local x 42
:put $x

Use /system script run in the terminal to test scripts before scheduling them.

Security Considerations

  • Scripts run with full administrative access — treat them like root commands
  • Avoid storing passwords in scripts when possible
  • Use /log liberally inside scripts so you have an audit trail

Summary

  • Scripts are stored under /system script and contain RouterOS CLI commands
  • The scheduler (/system scheduler) runs scripts automatically on a time or interval basis
  • Use :local, :if, :for, and :delay for logic and flow control
  • Common uses: daily backup, periodic reboots, interface restarts, failover triggers
  • Always test scripts manually before scheduling them