Community discussions

 
3r3d1
just joined
Topic Author
Posts: 4
Joined: Tue Aug 12, 2014 12:07 pm

Script job killer

Tue Aug 12, 2014 12:23 pm

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 2946
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: Script job killer

Tue Aug 12, 2014 8:14 pm

Solution:
/system script job remove [find];

Explanation:

$i is only a number, you must provide the ID, use "foreach".
I'm Italian, not English. Sorry for my imperfect grammar.
 
3r3d1
just joined
Topic Author
Posts: 4
Joined: Tue Aug 12, 2014 12:07 pm

Re: Script job killer

Wed Aug 13, 2014 12:57 pm

/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;
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 2946
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: Script job killer

Wed Aug 13, 2014 3:53 pm

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

:if ([/system script job get $id value-name=script] != "myscriptname") do={ <killing instructions> };
I'm Italian, not English. Sorry for my imperfect grammar.
 
3r3d1
just joined
Topic Author
Posts: 4
Joined: Tue Aug 12, 2014 12:07 pm

Re: Script job killer

Wed Aug 13, 2014 7:49 pm

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
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 2946
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: Script job killer

Wed Aug 13, 2014 8:06 pm

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


/system script job remove $id; !!!!!!!!!!!!!!
I'm Italian, not English. Sorry for my imperfect grammar.
 
3r3d1
just joined
Topic Author
Posts: 4
Joined: Tue Aug 12, 2014 12:07 pm

Re: Script job killer

Mon Aug 18, 2014 7:05 pm

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
 
User avatar
eworm
Member
Member
Posts: 378
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Script job killer

Thu Feb 21, 2019 7:14 pm

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

:if ([/system script job get $id value-name=script] != "myscriptname") do={ <killing instructions> };
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" ]
Manage RouterOS scripts and extend your devices' functionality: RouterOS Scripts

Who is online

Users browsing this forum: No registered users and 8 guests