Basic Scheduler and Scripts
Basic Scheduler and Scripts
Section titled “Basic Scheduler and Scripts”TL;DR (quick start)
Section titled “TL;DR (quick start)”For the impatient: here’s the 30-second version.
# Create and schedule a daily backup script/system script add name=daily-backup source="/system backup save name=auto-backup"/system scheduler add name=daily-backup-job start-time=00:00:00 interval=1d on-event=daily-backupOverview
Section titled “Overview”This guide demonstrates how to create scripts and schedule them on MikroTik RouterOS for automated tasks.
Prerequisites
Section titled “Prerequisites”- A MikroTik router running RouterOS 7.x or later
- Access to the router via SSH, WinBox, or WebFig
Configuration steps
Section titled “Configuration steps”Step 1: Create a Simple Logging Script
Section titled “Step 1: Create a Simple Logging Script”Create a script that logs the current date and time:
/system script add name=log-time source=":log info \"Scheduled check at \$[/system clock get time]\"" comment="Time logging script"Step 2: Create a Backup Script
Section titled “Step 2: Create a Backup Script”Create a script that generates a configuration backup:
/system script add name=daily-backup source="/system backup save name=auto-backup" comment="Daily backup script"Step 3: Schedule the Logging Script
Section titled “Step 3: Schedule the Logging Script”Schedule the logging script to run every hour:
/system scheduler add name=hourly-log interval=1h on-event=log-time comment="Run log-time every hour"Step 4: Schedule the Backup Script
Section titled “Step 4: Schedule the Backup Script”Schedule daily backup at midnight:
/system scheduler add name=daily-backup-job start-time=00:00:00 interval=1d on-event=daily-backup comment="Daily backup at midnight"Verification
Section titled “Verification”Check 1: Scripts
Section titled “Check 1: Scripts”/system script printExpected Output:
Flags: I - invalid # NAME OWNER POLICY RUN-COUNT LAST-STARTED 0 log-time admin read,write 5 jan/17/2026 14:00:00 1 daily-backup admin read,write 1 jan/17/2026 00:00:00Check 2: Scheduler
Section titled “Check 2: Scheduler”/system scheduler printExpected Output:
Flags: X - disabled # NAME START-DATE START-TIME INTERVAL ON-EVENT 0 hourly-log jan/17/2026 00:00:00 1h log-time 1 daily-backup-job jan/17/2026 00:00:00 1d daily-backupCheck 3: Test script execution
Section titled “Check 3: Test script execution”/system script run log-time/log print where message~"Scheduled check"Troubleshooting
Section titled “Troubleshooting”Problem: Script not running
Section titled “Problem: Script not running”Symptoms: Scheduled task never executes, no log entries.
Causes & Solutions:
-
Syntax errors in script - Test script manually:
/system script run log-timeIf error appears, fix the script source.
-
Scheduler disabled - Check scheduler status:
/system scheduler printLook for X (disabled) flag. Enable if needed.
-
Wrong script name in scheduler - Verify on-event matches script name exactly.
Problem: Script runs but nothing happens
Section titled “Problem: Script runs but nothing happens”Symptoms: Script executes (run-count increases) but expected action doesn’t occur.
Causes & Solutions:
-
Permission issues - Check script policy:
/system script print detail where name=daily-backupEnsure script has required policies (read, write, policy, test, etc.)
-
Variable scope issues - Local variables don’t persist. Use global for cross-script data.
-
Silent errors - Add logging to script for debugging:
:log info "Script started"# ... commands ...:log info "Script completed"
Problem: Script timing is wrong
Section titled “Problem: Script timing is wrong”Symptoms: Script runs at unexpected times.
Causes & Solutions:
-
System clock wrong - Verify NTP is configured:
/system clock print/system ntp client print -
Start-time not set - For specific times, set both start-time and interval:
/system scheduler set daily-backup-job start-time=00:00:00 -
Timezone issues - Check timezone setting:
/system clock print
Problem: Backup files not created
Section titled “Problem: Backup files not created”Symptoms: Backup script runs but no files appear.
Causes & Solutions:
-
Disk full - Check available space:
/file print/system resource print -
Path issues - Verify file destination:
/file print where name~"backup" -
Filename collision - Use unique names with date:
/system script set daily-backup source=":local date [/system clock get date]; /system backup save name=(\$date . \"-backup\")"
Related topics
Section titled “Related topics”Common Scripting Use Cases
Section titled “Common Scripting Use Cases”- System Backup - automate backups
- DHCP Server - lease notifications
- Netwatch - failover scripts
Related system topics
Section titled “Related system topics”- Logging - log script activity
- Email Tool - send notifications from scripts
- User Management - script permissions
Monitoring
Section titled “Monitoring”- Torch - traffic monitoring via scripts
- Resource Monitoring - CPU/memory checks