Community discussions

MikroTik App
 
mauricioesilva
just joined
Topic Author
Posts: 4
Joined: Sat May 23, 2015 10:57 pm

Problem with global variables with Scripts running from the scheduler

Fri Aug 21, 2015 11:27 pm

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?.
 
ditonet
Forum Veteran
Forum Veteran
Posts: 835
Joined: Mon Oct 19, 2009 12:52 am
Location: Europe/Poland/Konstancin-Jeziorna
Contact:

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

Tue Aug 25, 2015 12:15 am

Problem with global variables is not fixed for almost 4 years:
http://forum.mikrotik.com/viewtopic.php ... 3&p=293379

Regards,
 
marrold
Member
Member
Posts: 427
Joined: Wed Sep 04, 2013 10:45 am

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

Tue Aug 25, 2015 12:31 am

I don't have this issue, but I have no evidence currently to back it up...
 
mauricioesilva
just joined
Topic Author
Posts: 4
Joined: Sat May 23, 2015 10:57 pm

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

Tue Sep 22, 2015 1:36 pm

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.
 
User avatar
strods
MikroTik Support
MikroTik Support
Posts: 1661
Joined: Wed Jul 16, 2014 7:22 am
Location: Riga, Latvia

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

Tue Sep 22, 2015 3:09 pm

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"
 
mauricioesilva
just joined
Topic Author
Posts: 4
Joined: Sat May 23, 2015 10:57 pm

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

Fri Oct 02, 2015 12:25 am

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.