There is a bug with error reporting when executing scripts from a scheduler. If I run a script that contains a :return statement from the terminal, everything works correctly and no error is logged. However, if I run the same script from a scheduler, the script is also executed but an error appears on the log (executing script p from scheduler failed, please check it manually).
Script example:
/system/script/print where name=p
Flags: I - invalid
0 name="p" owner="sarenet" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon dont-require-permissions=no run-count=0
source=
:log info "EXECUTED";
:return "";
Log output:
2025-03-03 14:12:59 script,info EXECUTED
2025-03-03 14:12:59 script,error executing script p from scheduler failed, please check it manually
This issue should have been addressed in version 7.17 according to the changelog:
*) console - execute :return command without error;
However, it seems that it is not completely fixed. Have anyone also else experienced this?
PD: I know about /console settings log-script-errors option introduced with 7.18 but I can’t consider it as a clean solution.
Based on what you wrote, and guessing everything else…
The error is…
You omit “/system script run” in front of “p”. Is not just “p” but the full path must be specified:
/system script run p
Detailed:
What is p? Is a shortcut for print, so, for avoid misunderstanding, the scripts must be launched with /system script run,
because the scheduler script field is for a script, not just for a script name. Always has been.
The problem is that RouterOS has allowed you to only write the name of the script to launch it, but the various functions added to RouterOS have ended up interacting with :return which no longer knows who to return a result to what function, since it is used outside of a function to be interrupted.
According to Mikrotik documentation Scheduler - RouterOS - MikroTik Documentation, it is possible to specify only the name of the script and it should work. As I was saying previously, the script is executed but an error is logged.
but what's the point of a :return "" outside a loop that returns nothing to nothing?
I was a providing the most basic example to demonstrate the bug. I know that the script as of now is useless. Maybe this example with export is more clarifying:
/system/script/
add dont-require-permissions=no name=p owner=sarenet policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=\
"# Do logic\
\n:if (true) do={\
\n :log info \"EXECUTION MUST STOP\";\
\n :return \"\";\
\n}\
\n\
\n# Other logic"
/system scheduler
add interval=1m name=p on-event=p policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-time=startup
Log output:
2025-03-03 16:03:49 script,info EXECUTION MUST STOP
2025-03-03 16:03:49 script,error executing script p from scheduler failed, please check it manually
As you can see, I'm using :return as a solution to stop execution of the script. This is something commonly used in programming languages to early return if some condition is met. Nothing new.
I don't think is because of the name of the script. The name "p", once again, is for testing purposes. The name of my script is more complex. Nevertheless, thank you for the explanation about the "print" shorcut. I wasn't aware of that.
So, or your fix the scheduler, or you fix the script
:log info "PRE-RETURN"
:local useless do={ :return "" }
$useless
:log info "POST-RETURN"
>
I think this code is not valid for an early return. I have tested it and both messages are printed in log.
Thanks!
I was talking about using only the name of the script as the "on-event" parameter in the scheduler. I know the use of "return" to stop the execution is a little bit tricky but I don't know any other way to achieve an early return. Do you know any alternatives?
Yeah, you are totally right. If I set the "on-event" parameter with not only the name, it is working as expected. A little bit strange nevertheless. In my opinion, my solution should not log an error. I will have to reconfigure all my schedulers to avoid this problem.