Community discussions

MikroTik App
 
gotsprings
Forum Guru
Forum Guru
Topic Author
Posts: 2102
Joined: Mon May 14, 2012 9:30 pm

Is there a persistent script or only schedules?

Tue Mar 23, 2021 1:02 pm

I have been using scripts with scheduling for years. But a conversation with someone else made me wonder.

Application:
I have 2 ISP feeds and use a recursive routing table to pick between primary and secondary.

I have a script that checks when if the primary is active or not.

I assign a 1 or a 0.

Then I compare if the last result was 1 or 0. If the last state is the same... Nothing happens.

If the last state doesn't match... I flush the ip firewall connections. And change the global variable to the correct 1 or 0.

I was going to run this every 60 seconds.

Is there a way to have a script run persistent that takes action as soon as the status changes?

Or

Do I just stick with a script run by a scheduler?
 
accarda
Member Candidate
Member Candidate
Posts: 208
Joined: Fri Apr 05, 2019 4:06 pm
Location: Italy

Re: Is there a persistent script or only schedules?

Tue Mar 23, 2021 1:44 pm

You could run the script within a loop, so that if you have portion of it where you want to keep an initial state, you can do as those variables can be set outside the looping portion.
So for example you can embed your script with a code like this to make a loop:
:global stopScript false

:do {
.... all your code to put in a loop
.....
	:delay 1
} while=(!$stopScript)


At this point the advantage is that if you need some initialization variable or setting it can be done before the do statement; then inside your loop you will run things that will repeat at any cycle.
Also with last delay statement you can choose your cycle rate to run this over and over.
Then if you need to stop it, you can set the global var $stopScript to true and the script stops running.
Armando
 
gotsprings
Forum Guru
Forum Guru
Topic Author
Posts: 2102
Joined: Mon May 14, 2012 9:30 pm

Re: Is there a persistent script or only schedules?

Tue Mar 23, 2021 2:27 pm

While

Never used that command before.

Thanks for the pointer. Will have to try a few things with that.

Who is online

Users browsing this forum: No registered users and 23 guests