Scriptlet: Halt MikroTik scheduled scripts if multiple instances are detected.

The following script can be run in terminal (or via any automation tool that can login to your MikroTik devices via SSH) and checks for any duplicate script ‘jobs’ and kills them.

I wrote this after noticing a few of my scripts that use fetch would hang periodically and leave multiple jobs open.

#kill duplicate script jobs
:global counter
:global counter2
:foreach counter in=[/system script job find] do={
:global job [/system script job get $counter script]
:if ([:len [/system script job find where script=$"job"]] > 0 && [:len $job] > 0) do={
:put "Duplicate script running: $job - terminating all"
:foreach counter2 in=[/system script job find where script=$"job"] do={
/system script job remove $counter2
}
}
}

One solution to prevent this on the scheduler level is to create a check at the start of the scheduler entry to ensure only one instance of this script is running; this can be achieved by adding this block prior to running a script in your scheduler entry:

:global scriptname "YOURSCRIPTNAME"
:if ([:len [/system script job find script=$"scriptname"]] > 0) do={
:log info "$scriptname Already Running - killing old script before continuing"
:foreach counter in=[/system script job find script=$"scriptname"] do={
/system script job remove $counter
}
}
/system script run $scriptname
Advertisement

2 thoughts on “Scriptlet: Halt MikroTik scheduled scripts if multiple instances are detected.

  1. Hello, thank you for the solution.

    I donĀ“t understand why you use doble quote for variable:

    /system script job find script=$”scriptname”

    If I use …script=$scriptname.. it will be the same ?

  2. Hi Adrian – if the script name has odd characters/spaces/or commands that might otherwise be mis-interpreted it can help to wrap a variable in quotes to ensure the system knows this is still part of the variable in use.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.