I’ve read the proposed workaround at Problem with global variables with Scripts running from the scheduler
The below code fails in RouterOS 6.37.1:
- Create a global variable having a function
- Access that variable from a script (getting typeinfo or running it) then log the results
- Run the script from the schedule
- Wait for the scheduler to run
- Print log results
The code has been carefully crafted after reading http://wiki.mikrotik.com/wiki/Manual:Scripting and http://wiki.mikrotik.com/wiki/Manual:System/Scheduler but apparently I’m doing something wrong.
What?
The code below prints these log entries:
07:32:38 script,info direct execution of testFunction
07:32:38 script,info testFunctionScript
07:32:38 script,info testFunctionType=str
07:32:38 script,info testFunctionResult=1w6d01:00:04
07:32:38 script,info testFunction=;testFunction=(code)
07:32:38 system,info script removed by jeroenp
07:32:38 system,info new script added by jeroenp
07:32:38 script,info execution of testFunction via testFunctionScript
07:32:38 script,info testFunctionScript
07:32:38 script,info testFunctionType=str
07:32:38 script,info testFunctionResult=1w6d01:00:04
07:32:38 script,info testFunction=;testFunction=(code)
07:32:39 system,info script removed from scheduler by jeroenp
07:32:39 system,info new script scheduled by jeroenp
07:32:39 script,info execution of testFunction via testFunctionScriptSchedule calling testFunctionScript
07:32:59 system,info changed scheduled script settings by jeroenp
and this scheduler info:
Flags: X - disabled
0 X name="testFunctionScriptSchedule" start-date=sep/22/2015 start-time=12:02:37 interval=10s on-event=testFunctionScript owner="jeroenp" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive run-count=2
This means that:
- the direct execution of the function from the console works
- the execution of the function from a script works
- the scheduler executed the testFunctionScriptSchedule twice, but no information gets logged
The test code to reproduce this:
## logon as user jeroenp
/system script environment remove [ /system script environment find where name="testFunction" ];
:global testFunction do={
:local result [/system resource get uptime];
:return $result;
}
/system script environment print detail where name=testFunction
# 0 name="testFunction" value=";(eval /system scheduler (eval /localname=$result;value=(eval (eval /system resource getvalue-name=uptime))) (eval /returnvalue=$result))"
:log info "direct execution of testFunction"
{
:global testFunction;
:local testFunctionType [:typeof testFunction];
:local testFunctionResult [$testFunction];
:log info "testFunctionScript";
:log info "testFunctionType=$testFunctionType";
:log info "testFunctionResult=$testFunctionResult";
:log info "testFunction=$testFunction";
}
/log print where buffer=memory && (message~"testFunction" || topics~"info")
/system script remove [ /system script find where name="testFunctionScript" ];
/system script add name=testFunctionScript owner=jeroenp policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=":global testFunction;\r\
\n:log info \"testFunctionScript\";\r\
\n:local testFunctionType [:typeof testFunction];\r\
\n:local testFunctionResult [\$testFunction];\r\
\n:log info \"testFunctionType=\$testFunctionType\";\r\
\n:log info \"testFunctionResult=\$testFunctionResult\";\r\
\n:log info \"testFunction=\$testFunction\";\r\
\n"
:log info "execution of testFunction via testFunctionScript"
/system script run testFunctionScript
/log print where buffer=memory && (message~"testFunction" || topics~"info")
/system scheduler remove [ /system scheduler find where name="testFunctionScriptSchedule" ];
/system scheduler add interval=10s name=testFunctionScriptSchedule on-event=testFunctionScript policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive start-date=sep/22/2015 start-time=12:02:37
:log info "execution of testFunction via testFunctionScriptSchedule calling testFunctionScript"
:delay 20s
/system scheduler disable testFunctionScriptSchedule
/log print where buffer=memory && (message~"testFunction" || topics~"info")
/system scheduler print detail where name="testFunctionScriptSchedule"
–jeroen