Script job killer

Hi everyone I’m trying to create a script that kill all the jobs.

# Set var 
:local count 0
:local i 0
:local listid [:toarray [/system script job find]]

# Count number of jobs 
:foreach id in $listid do { 
    :set count ($count+1); 
    :log info $count;
}

# Set count -1 to prevent that the scipt kills himself
:set count ($count-1);

# in case that are present 3 jobs or more the killer start to delete
:if ($count > 3) do {
    :while ($i != $count) do {
        :system script job remove $i;
        :set i ($i+1);
      }
:log warning "The stuck spripts has been killed!";
}

The problem that I have is that the script doesn’t execute this line:
:system script job remove $i;

The script is working since that point, is that line wrong? on the terminal is working.

Thanks in advance.

Solution:
/system script job remove [find];

Explanation:

$i is only a number, you must provide the ID, use “foreach”.

/system script job remove [find]
Is working but it remove all the entry and the script is killing himself.
I used the variable $i to prevent this, when all the scripts that are stuck has been deleted the script i want to show a message in the log.

$i is only a number, you must provide the ID, use “foreach”.

Via terminal i can do “/system script job remove 0” and i thought that i could do it in a script.
How i can delete the script of row $i?

If i use a foreach it will still delete all of them

:foreach id in [/system script job find] do { 
    /system script job remove $id;
}

Inside foreach you must check the script name to prevent killing himself:

:if ([/system script job get $id value-name=script] != “myscriptname”) do={ };

I have tried like this but is not working

# Set var 
:local count 0

# Count number of jobs 
:foreach id in [/system script job find] do { 
    :set count ($count+1); 
}

# Set count -1 to remove this scipt from the count
:set count ($count-1);

# in case that are present 3 jobs or more the killer start to delete
:if (count > 3) do {
    :foreach id in [/system script job find] do{
        :if ([/system script job get $id value-name=script] != "jobs_killer") do={ 
            /system script job remove [find id=$id];
        }
    }
:log warning "The stuck scripts has been killed!";
}

Where is the mistake?

Thanks

/system script job remove [find id=$id]; ???


/system script job remove $id; !!!

Sorry about that the fact is that i tried different way as that /system script job remove $id; wasn’t working.

# Set var 
:local count 0

# Count number of jobs 
:foreach id in [/system script job find] do { 
    :set count ($count+1); 
}

# Set count -1 to remove this scipt from the count
:set count ($count-1);

# in case that are present 3 jobs or more the killer start to delete
:if (count > 3) do {
    :foreach id in [/system script job find] do{
        :if ([/system script job get $id value-name=script] != "jobs_killer") do={ 
            /system script job remove $id;
        }
    }
:log warning "The stuck scripts has been killed!";
}

I’m using RB2011 with in RouterOS 6.7.

Any idea why is not working?

Thanks

Even easier:

:foreach id in=[ / system script job find where script!="myscript" ] do={
  / system script job remove $id;
}

Or even more simple:

/ system script job remove [ find where script!="myscript" ]

Kill all but logins (e.g. self) and their child command jobs. Pre 7.16.1 it was enough to filter != login. The below depends on the return of [/system/script/job/find] being in order of job started, so parent is processed before a child.

As a one liner:

:local mVal ""; :local loginIds [:toarray ""]; :foreach id in=[/system/script/job/find] do={ :if ([/system/script/job/get $id type] = "login") do={ :set ($loginIds->([:tostr $id])) $id; } else={ :if ([:typeof ($loginIds->([:tostr [/system/script/job/get $id parent]]))] = "nothing") do={ :set mVal [/system/script/job/remove $id]; }}};

In a more human readable format with comments:

:local mVal ""; 
:local loginIds [:toarray ""]; 
:foreach id in=[/system/script/job/find] do={ 
	:if ([/system/script/job/get $id type] = "login") do={ 
		:set ($loginIds->([:tostr $id])) $id; ##Add the login job ID to array so we can check that no child belongs to a login parent
	} else={ 
		:if ([:typeof ($loginIds->([:tostr [/system/script/job/get $id parent]]))] = "nothing") do={ 
			:set mVal [/system/script/job/remove $id]; ##kill the job, does not have a parent that is of type login
		}
	}
};

But on that way you do not kill other login jobs…