Get name of script from inside script

I want to check if a script is already running from within the script. Is there a way to get the name of the script and check if it already running?
On the sidenote, when i print script jobs I alway see a script with no name. Does anyone know what it does?
[admin@MikroTik] > system script job print

SCRIPT OWNER STARTED

0 admin mar/10/2010 17:36:52
[admin@MikroTik] >

You can get script name form the script/job list

[admin@dzeltenais_burkaans] /system script> run getRestricted



[admin@dzeltenais_burkaans] /system script job> print 
 # SCRIPT                                 OWNER                                STARTED             
 0 getRestricted                          admin                                mar/12/2010 08:03:11
 1                                        admin                                mar/12/2010 08:03:14

Yes but how will I know if and which of the scripts currently running is the one I want? To be more specific, i don’t want the script to start (or exit immediately) if there is another script with the same name already running. Right know I count the number of scripts running and if the number is bigger than 1 then the script is already running ( i only run one script). But if I add another script for some other purpose I will have to change the number to 2 and still that will not be very precise. I want somehow to get the name of the script and check the number of times it is running.

easy:

{
/system script job
  :if ([:len [find script="getRestricted"]] > 0) do={
     :put "script already running";
  }
}

That is a step closer but what if the user changes the actual name of the script? I want my script to work under all circumstances, if that is possible.

AFAIK there’s no way to determine the script name from inside the script, there’s no $0 like in Perl etc.

Maybe check a global variable as the first step, if it’s set to true exit, if it’s set to false set it to true. And set it to false as the last step of the script. That approach could lead to problems if the script bombs in the middle as no script could run after that until the variable is manually fixed. Depending on what your script does that could be desirable behavior, though.

to work around that you can check how many scripts are running, and if only 1 is (one that checks how many scripts are running) set control values to false. Also, if script fails it should be fixed to not to fail, hence is i non-issue.

I thought so, just wanted to make sure. Thanx for the input.

Easier said than done. Let’s say I need to resolve DNS for my script:

[admin@MikroTik] /system script> pri
Flags: I - invalid 
 0   name="test" owner="admin" policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive run-count=0 
     source=:put "started"; :local test [:resolve doesnotexist.nowhere]; :put "ended" 
[admin@MikroTik] /system script> run 0
started
failure
[admin@MikroTik] /system script>

I can’t fix that in the script, DNS may simply be down at the time.

I found a solution to the ‘:resolve’ failure issue:
This script will set a global variable per host name being resolved. If resolve fails, no variable is created. It might offer a fix for the failing resolve command inside scripts.

If you find something better, please post, I’m trying to find any better solutions others may have.

:local hosts "google.com, somewhere.nowhere, another.domain"

:local execCmd
:foreach h in=[:toarray $hosts] do={
   :set execCmd (":global \"" . [:tostr $h] . "\" [:resolve \"" . [:tostr $h] . "\"]")
   :execute [:tostr $execCmd]
}