Page 1 of 1

Problem with global variables with Scripts running from the scheduler

Posted: Fri Aug 21, 2015 11:27 pm
by mauricioesilva
I'm running RouterOS 6.31 and all scripts fails to access the variables defined as global if the script runs from the scheduler. But if I run any script from the the CLI everything works fine.

I was using the No-Ip script ( http://wiki.mikrotik.com/wiki/Dynamic_D ... _No-IP_DNS ) in RouterOS version 6.2x and never see a problem until last update. After seen that the problem was the global variable used in it I made a small script to test the behavior of the variables and can't find the problem.

The test script it's simple, a bunch of put and log to debug it and the attributes are read,write and test (in CLI and from Scheduler).
---------
:global str1
:local str2 "something"

:put "Test: $str1"
:log info "Test: $str1"

:if ( $str1 != $str2 ) do={
:put "Mismatch"
:log info "Mismatch - $str1 - $str2"
:set $str1 $str2
} else={
:put "Equal"
:log info "Equal"
}
---------

The first run from the CLI it will print "Mismatch", and further executions will print the content of "str1" and the word "Equal" as expected. But if I make it run form the scheduler the global variable str1 will always be empty. It looks like it's destroy after each execution. I

Anyone having the same problem or I'm miss using the global variables?.

Re: Problem with global variables with Scripts running from the scheduler

Posted: Tue Aug 25, 2015 12:15 am
by ditonet
Problem with global variables is not fixed for almost 4 years:
http://forum.mikrotik.com/viewtopic.php ... 3&p=293379

Regards,

Re: Problem with global variables with Scripts running from the scheduler

Posted: Tue Aug 25, 2015 12:31 am
by marrold
I don't have this issue, but I have no evidence currently to back it up...

Re: Problem with global variables with Scripts running from the scheduler

Posted: Tue Sep 22, 2015 1:36 pm
by mauricioesilva
Thank you ditonet to point the other thread in the forum. Is good to know I'm not the only one with this problem, to bad nobody fix it.

I already do a workaround on the script making it run inside a "while" and "sleep" to replace the scheduler, but is not pretty.

Re: Problem with global variables with Scripts running from the scheduler

Posted: Tue Sep 22, 2015 3:09 pm
by strods
1) Create scheduler which has only script name in it.
2) Create global variable in CL "global test [/system resource get uptime]"
3) Create script where first line is only "global test" and then later use variable in script.

It works for me:
/system scheduler
add interval=10s name=test on-event=test policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive start-date=\
sep/22/2015 start-time=12:02:37

/system script
add name=test owner=admin policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive source=\
"global test\r\
\nlog info message=\$test"

Re: Problem with global variables with Scripts running from the scheduler

Posted: Fri Oct 02, 2015 12:25 am
by mauricioesilva
1) Create scheduler which has only script name in it.
2) Create global variable in CL "global test [/system resource get uptime]"
3) Create script where first line is only "global test" and then later use variable in script.

It works for me:
/system scheduler
add interval=10s name=test on-event=test policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive start-date=\
sep/22/2015 start-time=12:02:37

/system script
add name=test owner=admin policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive source=\
"global test\r\
\nlog info message=\$test"

Thank you strods for your answer. Your script works, and I use it as base for further testing. I have found that to read a global variable the script need to have the Policy "policy" without it it fail to get the variable (It have to be set on both, the script policies and in the scheduler policies).

First time I read the documentation about the policies (here: http://wiki.mikrotik.com/wiki/Manual:Scripting ) I did not see anything global variables, but the detail information (here: http://wiki.mikrotik.com/wiki/Manual:Ro ... Properties ) explain it a little better.