UNIXETC

RouterOS自动任务设置

概览

调度程序可以在特定时间或指定时间间隔之后触发脚本执行。

属性

备注

重新启动路由器将重置运行计数计数器。

如果必须同时执行多个脚本,它们将按照它们在调度程序配置中出现的顺序执行。如果一个计划脚本用于禁用另一个脚本,那么这个顺序就很重要。

如果需要更复杂的执行模式,通常可以通过调度多个脚本并让它们相互启用和禁用来完成。

Note: if scheduler item has start-time set to startup, it behaves as if start-time and start-date were set to time 3 seconds after console starts up. It means that all scripts having start-time is startup and interval is 0 will be executed once each time router boots. If the interval is set to value other than 0 scheduler will not run at startup

用例

每天凌晨4:30重启路由器

[[email protected]] >/system scheduler add name="autoReboot" on-event="/system reboot" start-time="04:30:00" interval="1d 00:00:00" disabled=no
[[email protected]] > /system scheduler print
Columns: NAME, START-DATE, START-TIME, INTERVAL, ON-EVENT, RUN-COUNT
# NAME        START-DATE   START-TIME  INTERVAL  ON-EVENT        RUN-COUNT
0 autoReboot  mar/12/2022  04:30:00    1d        /system reboot          0

我们将添加一个每小时执行脚本 log-test 的任务:

[[email protected]] system script> add name=log-test source=":log info message=test"
[[email protected]] system script> print
Flags: I - invalid
0 name="log-test" owner="admin" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon dont-require-permissions=no run-count=0
source=:log info message=test
[[email protected]] system script> .. scheduler
[[email protected]] system scheduler> add name=run-1h interval=1h
on-event=log-test
[[email protected]] system scheduler> print
Flags: X - disabled
# NAME ON-EVENT START-DATE START-TIME INTERVAL RUN-COUNT
0 run-1h log-test mar/30/2004 06:11:35 1h 0
[[email protected]] system scheduler>

在另一个示例中,将添加两个脚本来更改队列规则“Cust0”的带宽设置。每天上午 9 点队列将设置为 64Kb/s,下午 5 点队列将设置为 128Kb/s。队列规则、脚本和调度程序任务如下:

[[email protected]] queue simple> add name=Cust0 interface=ether1 \
\... dst-address=192.168.0.0/24 limit-at=64000
 [[email protected]] queue simple> print
 Flags: X - disabled, I - invalid 0 name="Cust0" target-address=0.0.0.0/0 dst-address=192.168.0.0/24
 interface=ether1 limit-at=64000 queue=default priority=8 bounded=yes
[[email protected]] queue simple> /system script
[[email protected]] system script> add name=start_limit source={/queue simple set \
 \... Cust0 limit-at=64000}
[[email protected]] system script> add name=stop_limit source={/queue simple set \
\... Cust0 limit-at=128000}
[[email protected]] system script> print
0 name="start_limit" source="/queue simple set Cust0 limit-at=64000"
owner=admin run-count=0
1 name="stop_limit" source="/queue simple set Cust0 limit-at=128000"
owner=admin run-count=0
[[email protected]] system script> .. scheduler
[[email protected]] system scheduler> add interval=24h name="set-64k" \
\... start-time=9:00:00 on-event=start_limit
[[email protected]] system scheduler> add interval=24h name="set-128k" \
 \... start-time=17:00:00 on-event=stop_limit
[[email protected]] system scheduler> print
 Flags: X - disabled
# NAME ON-EVENT START-DATE START-TIME INTERVAL RUN-COUNT
 0 set-64k start... oct/30/2008 09:00:00 1d 0
1 set-128k stop_... oct/30/2008 17:00:00 1d 0
[[email protected]] system scheduler>

下面的示例安排一个脚本,每周通过电子邮件发送路由器配置备份。

[[email protected]] system script> add name=e-backup source={/system backup
{... save name=email; /tool e-mail send to="[email protected]" subject=([/system
{... identity get name] . " Backup") file=email.backup}
[[email protected]] system script> print
0 name="e-backup" source="/system backup save name=ema... owner=admin run-count=0
 
[[email protected]] system script> .. scheduler
[[email protected]] system scheduler> add interval=7d name="email-backup" \
 \... on-event=e-backup
[[email protected]] system scheduler> print
 Flags: X - disabled
 # NAME ON-EVENT START-DATE START-TIME INTERVAL RUN-COUNT
0 email-... e-backup oct/30/2008 15:19:28 7d 1
[[email protected]] system scheduler>

不要忘记设置电子邮件设置,即 SMTP 服务器和 /tool 电子邮件下的 From: 地址。例如:

[[email protected]] tool e-mail> set server=159.148.147.198 [email protected]
[[email protected]] tool e-mail> print
 server: 159.148.147.198
from: [email protected]
[[email protected]] tool e-mail>

下面的示例将从午夜到中午每小时将“x”放入日志中:

[[email protected]] system script> add name=enable-x source={/system scheduler
{... enable x}
[[email protected]] system script> add name=disable-x source={/system scheduler
{... disable x}
[[email protected]] system script> add name=log-x source={:log info message=x}
[[email protected]] system script> .. scheduler
[[email protected]] system scheduler> add name=x-up start-time=00:00:00 \
\... interval=24h on-event=enable-x
[[email protected]] system scheduler> add name=x-down start-time=12:00:00
 \... interval=24h on-event=disable-x
[[email protected]] system scheduler> add name=x start-time=00:00:00 interval=1h \
\... on-event=log-x
[[email protected]] system scheduler> print
Flags: X - disabled
 # NAME ON-EVENT START-DATE START-TIME INTERVAL RUN-COUNT
0 x-up enable-x oct/30/2008 00:00:00 1d 0
1 x-down disab... oct/30/2008 12:00:00 1d 0
2 x log-x oct/30/2008 00:00:00 1h 0
[[email protected]] system scheduler>