[SOLVED] Script does not execute via Scheduler on startup

Hello,

I’m trying to debug a problem of a script that seems to work correctly when the router is already running, but does not seem to actually do anything when run in the scheduler on startup. I can see in

/system scheduler

that the run count has increased to 1 when I reboot the router but nothing else happens.

Here is the script source:

/system script
add dont-require-permissions=no name=enable-wan-netwatch owner=admin policy=\
    read,write,policy,test,password,sniff,sensitive source=":local wangateway \
    \"ether8-gateway\";\r\
    \n:local waniface \"\";\r\
    \n#:local ct 0;\r\
    \n:local waniface [/interface detect-internet state get [:pick [find state\
    =\"internet\"] 0] value-name=name]\r\
    \n\r\
    \n:while (\$waniface != \$wangateway) do={\r\
    \n\t:nothing\r\
    \n\t#:log info \$waniface\r\
    \n\t#:set ct (\$ct+1);\r\
    \n}\r\
    \n\r\
    \n/tool netwatch enable [find where host=\"8.8.8.8\"]"

Here is the scheduler entry:

add comment="Enable WAN Netwatch on Reboot for Primary Router" name=\
    netwatch-enable on-event="/system script run enable-wan-netwatch" policy=\
    read,write,policy,test,password,sniff,sensitive start-time=startup

The script & scheduler policies seem to be identical and I have tried setting the on-event in the scheduler to just “enable-wan-netwatch” as well, but so far nothing works. Any help in figuring out what I’m doing wrong is very much welcomed!

Version info:
[] Router: CCR1009-8G-1S-1S+
[
] ROS version: 6.48

The script is run when the interface is not yet available. Just add a delay.

Hello. Actually I want the script to keep looping when the interface is not available, hence the “while ($waniface != $wangateway)” check. But I’m not sure what is happening as the subsequent “/tool netwatch enable” command does not seem to run either.

Once entered the script will never leave the loop as neither $waniface nor $wangateway is modified inside.

Please keep in mind that internet detect feature can cause lot of trouble. I would recommend not to use it.

Yes, I realised that this logic flaw and modified the while loop slightly. This is the new loop:

:while ($waniface != $wangateway) do={
	#:nothing
               :local waniface [/interface detect-internet state get [:pick [find state="internet"] 0] value-name=name]
}

However, in both case after the router reboot I do not see anything running in “/system scheduler jobs” which is strange.

That is frustrating. I am hoping to keep the script simple as it just needs to enable a netwatch check after the router gets a IP. I really would prefer to not have to write my own logic to check if network is available :frowning:

With :local inside the loop you limit the scope of the variable. Initialize it before, then use :set inside loop.

Will have to check this on real hardware, currently typing on my mobile.

Wondering if your solution is over-complicated… How about something like this?

:while ([ :len [ / interface detect-internet state find where state=internet ] ] = 0) do={
  :delay 200ms;
}
# your follow-up code here...

If you really have to check for the interface just add name=“ether8-gateway” to the find command.

Thanks for the recommendation eworm. I could not get it to work with the variable check and in fact even the “:set $variable” I had in the loop was not working correctly. Once I modified the script to use the :len check logic you provided, it started working correctly.

The other gotcha for me was I had the script/scheduler policy set to “read/write/policy/test” only but for netwatch commands to execute in a script, you need to add “reboot” Policy as well.