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
/system script add name=my-first-script source={
/log info message="Hello from script!"
}Running a Script Manually
/system script run my-first-scriptViewing All Scripts
/system script printScript Syntax Basics
RouterOS uses its own scripting syntax:
Variables
:local myVar "Hello"
:log info $myVarConditional Logic
:if ([:ping 8.8.8.8 count=1] = 0) do={
:log warning "Google DNS unreachable"
} else={
:log info "Google DNS reachable"
}Loops
:for i from=1 to=5 do={
:log info ("Iteration: " . $i)
}String Concatenation
:local ip "192.168.1.1"
:log info ("Gateway is: " . $ip)Practical Script Examples
Example 1: Daily Backup
/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
/system script add name=clear-dns source={
/ip dns cache flush
:log info "DNS cache cleared"
}Example 3: Restart a Specific Interface
/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
/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.
/system schedulerAdding a Scheduled Task
/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 taskinterval— how often to run (1d= daily,1h= hourly,00:05:00= every 5 minutes)start-time— time of day to first runstart-date— optional specific start dateon-event— the command or script to run
Schedule a Stored Script
/system scheduler add name=run-backup interval=1d start-time=02:00:00 on-event="/system script run daily-backup"Schedule Every 5 Minutes
/system scheduler add name=check-dns interval=00:05:00 on-event="/system script run clear-dns"Schedule a One-Time Task
/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
/system scheduler printExample output:
# 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-backupUseful Built-in Functions
Get Current Time
:local now [/system clock get time]
:log info ("Current time: " . $now)Get Interface Status
:local status [/interface get ether1 running]
:if ($status = true) do={ :log info "ether1 is up" }Find and Modify Objects
/ip route set [find dst-address="0.0.0.0/0" comment="Primary WAN"] distance=10The [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:
:local x 42
:put $xUse /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
/logliberally inside scripts so you have an audit trail
Summary
- Scripts are stored under
/system scriptand contain RouterOS CLI commands - The scheduler (
/system scheduler) runs scripts automatically on a time or interval basis - Use
:local,:if,:for, and:delayfor logic and flow control - Common uses: daily backup, periodic reboots, interface restarts, failover triggers
- Always test scripts manually before scheduling them
