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
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.
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 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.