The topic was probably discussed, but I want a short and beautiful solution. How to prevent a script from running if it is already running?
In this case, the executable script itself must perform the check.
It depends from where it is invoked, for scheduled scripts which may not have completed I use the following for on-event
:local name “unms_update_ip”
:if ([/system script job print count-only where script=$name] = 0) do={
/system script run $name
} else={
:log info “$name already running”
}
No matter what is the script name, just make unique the UniqueScriptID…
:local UniqueScriptID "QnJhdm8h"
:local ThisScriptName [/system script get ([find where source~"$UniqueScriptID"]->0) name]
:local AlreadyRunning "Script $ThisScriptName already running"
:if ([:len [/system script job find where script=$ThisScriptName]] > 1) do={:log error $AlreadyRunning; :error $AlreadyRunning}
# simulating script running for 60 seconds
:delay 60s
Rex, I’m sorry I didn’t answer right away. Some circumstances distracted me. Thank you for the code. I never thought that this is how you can solve the problem.
One more question, if I may. But what if you need to block from restarting not a script, but a function? After all, when the function works, it is not in /system script job …
Yes, but… I can not understand… one concrete example?
Calling the same function twice from the same script or from another script?
Do you want it to run only once at a time, regardless of which script calls the GLOBAL function?
The problem, in that case, is when for some reason the function terminates without signaling that it is closed.
Any flag that is running remains set and the function cannot be called again until you manually remove the flag…
until you manually remove the flag…
or the script itself. Thank you very much, Rex, I got it.
Perfect… ![]()