how to stop a scheduled script after x times?

I have this script that runs after reboot for several times to check something.
But after the check is done and the result processed the script is no more of use, unil in the future router boots again.

I can start the script in scheduler but how to end it from running after “x” tims or “xx xx xx” time?
Next time router boots scheduler start script again. So removing the script is not the best methode.
Next reboot the script needs to be there waiting to be executed…

Hi,

counter in :global variable?

this runs script 5 times:

:global counter
:set counter ($counter+1)
:if ($counter <= 5) do={:log info run}

In the script check the run-count of the scheduler and then disable it.

Something like the untested example below.

:local cnt [/system scheduler get [/system scheduler find name=<scheduler-name>] run-count]

:if(cnt>x) do={/system scheduler disable [/system scheduler find name=<scheduler-name>]}

I think this disables this specific scheduler. But it has to be back up after a reboot of the router. So how to do that…

as petrn mentioned. Set up global variable that stores run counts.
If count is less than x then execute your script.

What about to run another script once after boot to enable that script schedule?
I wonder when someone ask for codemorphing script

Hi,

found major issue in V.1:

# V.2 
# Changelog $counter overflow after 2^31 runs
:global counter
:set counter ($counter+1)
:if ($counter <= 5) do={:log info run} else={:set counter ($counter-1)}

I’m always saying “get the requirements correct before coding!” :open_mouth:

Ok, as being a complete dummy in this I ask you where to put your lines in our (I am not the editor) script of subject.

:local delais 5s
:local liste 5mhz,10mhz,20mhz;
:foreach band in=$liste do={
:if (![/interface wireless get wlan1 running]) do={
/interface wireless set wlan1 channel-width=$band wireless-protocol=any
:delay $delais
}
}

Your cooperation is very much appreciated.

  1. put whole script in place of “:log info run”
  2. or more cleaner: replace “:log info run” with “/system script run ”, an put script in separate entry under “/system script”
  3. couple of dozen other ways to do that …

i am just wondering why not just wrap script in for loop that runs script as many times as required - so, start script just once, that will actually do the loop of original.

results - scheduled script is always enabled, it runs certain times as set by for loop.

else just make check, for value of some variable, that gets incremented when script is ran. Then before running the script, just check weather certain value is smaller than run count.

something like this:

1st scheduler runs once at startup and initialized global variable (counter) to 0
2nd scheduler starts at startup with small delay with interval. check if variable is set (if it is not set, it will be empty, else, it will hold some value you set in init scheduler, and increment counter. when counter value is over some certain value do not execute. (one if clause to check if variable is set, second, to check if variable is in defined parameters.

It will be ran after each interval, but 2 if clauses will consume negligible amount of resources.

Nice, example please?
I still have problems to get it to work.

Why can’t MT not change scheduler anyway? We already have an option “Start Time” (and date) that can be set to “startup” and we can set the interval.
Why not add a stop time (which can be real time) and/or an option that stops the scheduler after X runs?
This would make lots of things so much easier for dummies like me…

Another question: the “On Event” field in scheduler; Is this only to be used to fill in a script that is going to be executed? Or can this field in itself also execute code?

What I have done now is the following:
I have scheduler set to run the script that ´petrn´ made with the counter.
His script then starts a second script that I want to be executed and terminated after X times.

It doesn’t work, script just runs endless…

/system scheduler
add disabled=no interval=20s name=schedule1 on-event=Limit_ChangeBand_to5 policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api start-time=startup



/system script
add name=ChangeBand policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api source=":local delais 5s\r\
    \n:local liste 5mhz,10mhz,20mhz;      \r\
    \n:foreach band in=\$liste do={\r\
    \n:if (![/interface wireless get wlan1 running]) do={\r\
    \n/interface wireless set wlan1 channel-width=\$band wireless-protocol=nv2-nstreme-802.11\r\
    \n:delay \$delais\r\
    \n}\r\
    \n}"
add name=Limit_ChangeBand_to5 policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api source=\
    ":global counter\r\
    \n:set counter (\$counter+1)\r\
    \n:if (\$counter <= 5) do={/system script run ChangeBand} else={:set counter (\$counter-1)}"

But this is obviously not working… :frowning:

Hi WirelessRudy,

do you reboot router while testing? Or at least are you removing “counter”:

/system script environment remove [find name=counter]

Your scripts/scheduler works fine in my ROS V4 and ROS V5.

/system scheduler
add disabled=no interval=20s name=schedule1 on-event=Limit_ChangeBand_to5 policy=
ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api start-time=startup

>

The on-event needs to be a valid ros command or commands like:

```text
log info hello
/system script run "Limit_ChangeBand_to5"
log info goodbye
...
..

Hope this helps.

like reverged noted - on-event and any other place like that where ROS executes something for you, you should fill in correct RouterOS commands. easiest way to do this is to use CLI and open, for example script, for editing and check the mark-up - if it is there and no error is indicated, script will run.

/sy script edit source

Yes, I did reboot. It still doesn’t work

The scheduler works fine. It executes the script named “Limit_ChangeBand_to5” so I don’t understand the other post in this topic.
The script “Limit_ChangeBand_to5” (which is just the name of the script) runs as well (counter goes up and stops if I disable scheduler and runs again if I enable scheduler)

But the script “Limit_ChangeBand_to5” should now execute the script “ChangeBand” which is not happening. Also, the script “Limit_ChangeBand_to5” just runs endless…

Can you give me a print of your script?

Actually it can be script like you suggest, but this scheduler

/system scheduler
add disabled=no interval=20s name=schedule1 on-event=Limit_ChangeBand_to5 policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api start-time=startup

works fine, as is it described here: http://wiki.mikrotik.com/wiki/Manual:System/Scheduler

Why you just simply test that?:

/system script
add name=schedtest policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api source=\
    ":log info \"It works...\""
 /system scheduler
add disabled=yes interval=2s name=schd1 on-event=schedtest policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api
add disabled=yes interval=2s name=schd2 on-event="/system script run schedtest" policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api

Paste this into your router, both schedulers are intentionally disabled - you need to run only at time.
And now a bit of troubleshooting:

  1. run script manual from /system script by selecting “schedtest” script and clicking “Run Script”
  2. look in log for message “It works…”, if you don’t see you have find what’s wrong, no to try schedulers
  3. enable one scheduler, look for same message in log
  4. disable scheduler, enable other one, look for message in log

I think this is just common sense.

I have used your script as you posted them here in my RB493 / ROS v5.4, they work fine, i can see band changing in winbox. I think from information given here no one can make why it fails on your router …
Are you aware that if wlan interface is “R-running” - script will not touch band configuration? So might look like scripts are not working.

Well, I think I have to rephrase because my issue is NOT the scheduler, it is the script that should limit it running after x many times…

So this works fine:

/system scheduler
add disabled=no interval=20s name=schedule1 on-event=Limit_ChangeBand_to5 policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api start-time=startup

Now, the ´On event´ mentioned script “Limit_ChangeBand-to5” that is initiated is also executed.
So far, so good… BUT, it is not limiting the scheduler from running after X many times and also it is not initiating another script.
It is running but not working:

name=Limit_ChangeBand_to5 policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api source=\
    ":global counter\r\
    \n:set counter (\$counter+1)\r\
    \n:if (\$counter <= 5) do={/system script run ChangeBand} else={:set counter (\$counter-1)}"

Now, the next script “ChangeBand” works fine executed manually, but is not initiated by this “Limit_ChangeBand-to5” script.

So obviously, someting in this code is wrong:

“:global counter\r
\n:set counter ($counter+1)\r
\n:if ($counter <= 5) do={/system script run ChangeBand} else={:set counter ($counter-1)}”

In plain wordings:
+++++
Scheduler executes after reboot script “A” every xx secs
script “A” runs and should start script “B” if counter is less than 5 and if not should stop scheduler to make script run.
script “B” is not running. (It is just not initiated by script “A”. If initiated manually or direct by the scheduler is runs fine and does do what it is supposed to do.)
++++
Script “A” is not doing what it is supposed to do. It should stop the scheduler to run after X many times.

One question: How does the script “A” with code

:if (\$counter <= 5) do={/system script run ChangeBand} else={:set counter (\$counter-1)}

work?
The way I read it translates into: “If counter is less than 5 run the “ChangeBand” script, and if not (so bigger or equal 5) then ???”

I don’t understand what the code

{:set counter ($counter-1)}

is doing? What it should do is to stop the scheduler to run this script, or to stop this script itself all by itself…


And by the way, where are all these "\r" and “\n” coming from? They weren’t there in my original script. What do they do?

I think actually now it is impossible to stop a scheduler from running without disabling it.
MT should make that option in it as mentioned before.

The reason is that I am tidy and don’t like it when a script runs eternally while it has no more function…, but it only has to start again at the next reboot.

i believe that to “run script x-times after every boot” and “enabling scheduler after boot, running script x time, then disabling scheduler till next reboot” is from “ChangeBand” script point of view same - this script will be started x-times. If you want to talk about resource usage, of course there are differences: my scheduler runs all time, so there is CPU usage, but not single FLASH write. Disabling/enabling scheduler will save some CPU cycles, but generate FLASH writes. You have to decide what is better. I think i can write set of script enabling/disabling scheduler too.

One question: How does the script “A” with code

:if ($counter <= 5) do={/system script run ChangeBand} else={:set counter ($counter-1)}

>
> work?
> The way I read it translates into: "If counter is less than 5 run the "ChangeBand" script, and if not (so bigger or equal 5) then ?????"
>
> I don't understand what the code
>
> > {:set counter (\$counter-1)}
>
> is doing? What it should do is to stop the scheduler to run this script, or to stop this script itself all by itself...

I am so sorry for this, i was meant as joke. Assuming counter is signed 32bit integer, and you run scheduler every 20seconds, it will take  497,102 days (1,361 years) to overflow,:set counter ($counter-1) takes care of that - you can safely delete that part, script will still work.

> And by the way, where are all these "\r\" and "\n" coming from? They weren't there in my original script. What do they do?

are they in your script in winbox when you open script for editing? No, they only appear when you do export, **/system script export**, that's the way MT exports scripts

> I think actually now it is impossible to stop a scheduler from running without disabling it.
> MT should make that option in it as mentioned before.
>
> The reason is that I am tidy and don't like it when a script runs eternally while it has no more function...., but it only has to start again at the next reboot.

You first post had not mentioned that. You wanted to run some script x-times after every boot. And that is what my script is exactly doing, copy and paste this in winbox terminal window (and you might need to press enter to submit last line):

```text
/system script
add name=A policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api source=\
    "log info \"Script A start\"\r\
    \n:global counter\r\
    \n:set counter (\$counter+1)\r\
    \n:if (\$counter <= 5) do={/system script run B}\r\
    \n:log info \"Script A end\""
add name=B policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api source=\
    ":global counter\r\
    \n:log info (\"Script B run number:\".\$counter)"
add name=simulatereboot policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api source=\
    ":global counter 0"
/system scheduler
add disabled=no interval=3s name=schedule2 on-event=A policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api start-date=jun/17/2011 \
    start-time=13:15:16

simulatereboot script will reset counter, so you don’t have to reboot. And you can watch messages in winbox Log window.
That is my last attempt to prove that this script is working. If you want me to write scheduler enable/disable stuff, let’s talk outside forum.

BTW messing with wifi config every boot doesn’t seems too tidy to me.