Get script name or prevent script from beeing executed again when it is still running

Hi,

i’m searching for a solution to prevent the execution of a script when it is still running. My first idea was to search for the script name with:

/system script job find where script=NameOfMyScript

But that is not enough sophisticated for me :slight_smile: It would be great to get the name of the script that calls this command, but i can’t find a way to get that information automatically. Is there a way or variable to do that?

From within the script itself, you could set a global variable to track whether the script is running or not.

:global running
:if ($running != true) do={
  :set running true
  ... script content here
  :set running false
}

And what if the script crashes meanwhile? It will not run but the variable will remain true…

A semaphore (this variable thing) was my first idea, but it is very unstable as jarda says and you need at least another startup script that sets the variable to a fixed value at startup.
The next disadvantage is more source code and you need to set the variable back at every endpoint of the script. That is errorprone :wink:

I think i will do it with hard coded script names for now. I do not like it, but it is the less ugly way…


If anyone knows how to get the script name from within the script, please post it here!

Here is the - not so perfect - script :wink:

# Variables - Part 1
:local ScriptName "gateway0-check" 

# Script already running? If yes, log message and exit.
:if ([:len [system script job find where script=$ScriptName]] > 1) do={
	:log info ($ScriptName . " - script still active. Termination of another execution.")
	end
}

# Variables - Part 2
# ... rest of the script