Page 1 of 1

prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 9:45 am
by Techsystem
Hello My friends..!
so i have a script run on my mikrotik router, and the problem with it is that i dont want from it to return to scratch after my router reboot, so i want it to resume as it end after reboot.
so how can i do that..?
in a nutshell : i don't need this script to be affected by the reboot, but i want from it to keep normaly .
in this screenshot what all this check box mean..?

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 11:39 am
by mkx
If I understand your question correctly: reboot doesn't preserve router's state in general and state of a running script is no exception. (IMO that's one of crucial points of rebooting the device in the first place). So ROS, after reboot, has no idea if script was running when rebooted, it has no idea which command inside script was running and if it was sleep ROS has no idea when command was supposed to terminate. So the best newly booted ROS can do is to stick to schedule and start script next time it's scheduled to do so.

If you really want to run the script in such long time intervals, you have to make script remember its state somehow (I can think of a few ways) and then run script more frequently ... at each script run it would check current state and act accordingly.

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 1:01 pm
by Techsystem
[moderator action]
exactly that's what i want .
you have to make script remember its state somehow (I can think of a few ways) and then run script more frequently
so how can i achieve that ..? can you give me just an example for this situation here is my script.
/routing rule enable number=0
/routing rule disable number=2
:delay 1440m
/routing rule enable number=2
/routing rule disable number=0
i want from it to run forever..with no interruption.
in some way i want from my router to remember the last state -(i mean if my router in second section ((/routing rule enable number=2
/routing rule disable number=0)) if he rebooted i want from it to continue here not to begain from the begining ).

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 2:22 pm
by trinidad
I have disabled the scheduled "power cycles" on the wifi outlets powering the Aimesh nodes and router.

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 2:26 pm
by pe1chl
You can add a script that is started at bootup and in that script you can disable the other ones.

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 2:57 pm
by optio
/routing rule enable number=0
/routing rule disable number=2
:delay 1440m
/routing rule enable number=2
/routing rule disable number=0
If this is exact case, you don't need persistence, you can just toggle enabled/disabled state on rules.
/routing/rule
set disabled=(![get value-name=disabled 0]) 0
set disabled=(![get value-name=disabled 2]) 2
i want from it to run forever..with no interruption.
You can put code into scheduler with 1440min interval and startup time.
in some way i want from my router to remember the last state -(i mean if my router in second section ((/routing rule enable number=2
/routing rule disable number=0)) if he rebooted i want from it to continue here not to begain from the begining ).
Generally you can achieve persistence by writing into files or comments, but for your example it is not needed.

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 3:19 pm
by Techsystem
[moderator action]
You can put code into scheduler with 1440min interval and startup time.[/b]
can you explain more..?

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 3:42 pm
by optio

/routing/rule
set disabled=(![get value-name=disabled 0]) 0
set disabled=(![get value-name=disabled 2]) 2

so what you mean by this rule..? what it is going to do..?
It will switch disabled state of rules, if rule is disabled it will switch to enabled and vice versa. You just need initially to set states of rues different, eg: 0 - enabled, 2 - disabled. Rules will then switch states when executed.
You can put code into scheduler with 1440min interval and startup time.
can you explain more..?
This will create scheduler for 24h interval (1440min) started on router boot (startup) with script code from above:
/system/scheduler add name=switch-route-rules interval="1d 00:00:00" start-time=startup on-event="/routing/rule; set disabled=(![get value-name=disabled 0]) 0; set disabled=(![get value-name=disabled 2]) 2"
Refer to scheduler manual if you need additional info for it: https://wiki.mikrotik.com/wiki/Manual:System/Scheduler
If you really need to be exactly 24h interval even if rebooted, then create scheduler with start-time on specific time eg: start-time="00:00:00" (midnight) instead startup, then it will switch rules every midnight.

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 4:13 pm
by rextended
A simple description of what you want is better than looking for a solution to a problem that attempts to solve the primary need.
The classic XY Problem

So you want a script that alternates used routes every day?
1) When you restart the routerboard it remains the same combination as it was before it shut down.
2) If you change the lines every day at 04:00 in the night, so as not to interrupt at midnight, it is better
3) Just schedule two events, one that sets route A at 04:00 in the night,
and one that set course B the other night.
4) If you set something that just reverses, it could be the wrong day.

5) Yur script is wrong, never use number= because numbers can vary and work only on terminal.
6) And also [get value-name=disabled 0] is wrong, for the same reason.
No matter if sometime work or not.

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 4:28 pm
by rextended
can you explain more..?

Add to the rule "0" the comment "Rule A" (without quotes)
Add to the rule "2" the comment "Rule B" (without quotes)

Paste this on terminal.
/system scheduler
add interval=2d name=enable-routing-rule-a on-event="/routing rule\r\
    \nenable [find where comment~\"Rule A\"]\r\
    \ndisable [find where comment~\"Rule B\"]\r\
    \n" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=apr/01/2023 start-time=04:00:00
add interval=2d name=enable-routing-rule-b on-event="/routing rule\r\
    \nenable [find where comment~\"Rule B\"]\r\
    \ndisable [find where comment~\"Rule A\"]\r\
    \n" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=apr/02/2023 start-time=04:00:00
Done.

On reboot the routerboard keep same previous routing rule enabled, and everynight @04:00 (AM) switch the two rules.
Since the schedulers are on two different days, the rules are switched correctly.

Do you also need something that if the routerboard is off at 04:00 (AM) still syncs the right rule at the start without waiting for 04:00 the next day?

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 4:34 pm
by optio
5) Yur script is wrong, never use number= because numbers can vary and work only on terminal.
6) And also [get value-name=disabled 0] is wrong, for the same reason.
No matter if sometime work or not.
True, but it covers example for OP question, I didn't bother to explain how to create script that identify rules, eg. by setting some comment on them and find them by it. Since it is seems it will raise more questions about how it works, I'm just lazy (or have more important things to spend time) :)
There is also edge case if router is down at schedule time, to cover that, schedule which is executed at specific time of a day that is switching rules will need to set to some persistent variable of last execution time and addidional startup script will need to be created to check that persistent variable time and if is not in current day will need to execute switching rules...

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 4:41 pm
by Techsystem

/routing/rule
set disabled=(![get value-name=disabled 0]) 0
set disabled=(![get value-name=disabled 2]) 2

so what you mean by this rule..? what it is going to do..?
It will switch disabled state of rules, if rule is disabled it will switch to enabled and vice versa. You just need initially to set states of rues different, eg: 0 - enabled, 2 - disabled. Rules will then switch states when executed.
You can put code into scheduler with 1440min interval and startup time.
can you explain more..?
This will create scheduler for 24h interval (1440min) started on router boot (startup) with script code from above:
/system/scheduler add name=switch-route-rules interval="1d 00:00:00" start-time=startup on-event="/routing/rule; set disabled=(![get value-name=disabled 0]) 0; set disabled=(![get value-name=disabled 2]) 2"
Refer to scheduler manual if you need additional info for it: https://wiki.mikrotik.com/wiki/Manual:System/Scheduler
If you really need to be exactly 24h interval even if rebooted, then create scheduler with start-time on specific time eg: start-time="00:00:00" (midnight) instead startup, then it will switch rules every midnight.
but in this case we didn't get rid from our problem.
to explain more.
i have 200G bit suscription from each ISPs provider for each month, my script tell the router to get internet from ISP-1 for the first day then from ISP-2 for the second day, then back to run the script from the begaining ---> ISP-1 active..etc -(as you see the interval is 2d and the separation time between both is 1d)-
my problem is when the router is rebooted -(sometime every 14 hours so less than one day so the other part of the script is off)- the script goes back from the begaining so the other ISP is off, in your scenario if the router is rebooted your script will run, and it will revserse my script, but that will return us back to the same problem but in this case with ISP-2 not ISP-1.
i think youe understand what i mean now.
so in the day 15 of the month my ISP-1 supscription is end and my router with no internet until i switch it manually.

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 4:50 pm
by optio
in your scenario if the router is rebooted your script will run
It will not if you don't set start-time to startup, instead set to some specific time of a day, that's why I wrote at the end:
If you really need to be exactly 24h interval even if rebooted...
I just set there midnight to be example, set time that is best fit for you.

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 4:53 pm
by mkx
but in this case we didn't get rid from our problem.

Your explanation fits proposal by @rextended perfectly.

The only exception: it doesn't start every month so that 1st day ISP1 is used ... it depends on number of days in previous month. Example uses ISP1 from 1st of April then it alternates ISP every day. So 30th of April is ISP2 and 1st of May is ISP1 again. But at the end of May things change: 30th of May is ISP2, 31st of May is ISP1 and 1st of June is ISP2 ... but then, the script ensures ISP alternation as long as router is up & running at selected time of running the script.

Now, if you really follow principle by @rextended, but change it only slightly:
  1. move contents of "on-event" for odd days to a script called "ISP-odd-days"
  2. move contents of "on-event" for even days to a script called "ISP-even-days"
  3. create scheduler entries to run appropriate script ... you can run it multiple times as the script doesn't blindly toggle disabled flag for routes but it rather sets flags in explicit way. So you can create multiple scheduled jobs and ISP lines will get toggled if router will be running at any of execution times:
    /system scheduler
    add interval=2d name=enable-routing-rule-a-take1 on-event=ISP-odd-days policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=apr/01/2023 start-time=04:00:00
    add interval=2d name=enable-routing-rule-a-take2 on-event=ISP-odd-days policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=apr/01/2023 start-time=04:15:00
    add interval=2d name=enable-routing-rule-a-take3 on-event=ISP-odd-days policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=apr/01/2023 start-time=04:30:00
    add interval=2d name=enable-routing-rule-a-take4 on-event=ISP-odd-days policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=apr/01/2023 start-time=04:45:00
    
    add interval=2d name=enable-routing-rule-b-take1 on-event=ISP-even-days policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=apr/02/2023 start-time=04:00:00
    add interval=2d name=enable-routing-rule-b-take2 on-event=ISP-even-days policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=apr/02/2023 start-time=04:15:00
    add interval=2d name=enable-routing-rule-b-take3 on-event=ISP-even-days policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=apr/02/2023 start-time=04:30:00
    add interval=2d name=enable-routing-rule-b-take4 on-event=ISP-even-days policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=apr/02/2023 start-time=04:45:00
    
    So if router will be running at any of times indicated, ISP lines will be swaped.

Re: prevent schedule from restarting after reboot  [SOLVED]

Posted: Mon Apr 10, 2023 5:17 pm
by rextended
Ok.... understand........

One unique scheduler, no matter when the routerboard is rebooted or for how many time is off.
No global variable, file or other frills needed. Just set the correct comments on Rule A and Rule B.

If the day is even, activate B, if it is odd, activate A.
Check constantly, change only if necessary.
/system scheduler
add interval=1m name=enable-daily-routing-rule on-event="/routing rule\r\
    \n:if (([:pick [/system clock get date] 4 6] % 2) = 0) do={\r\
    \n    disable [find where (comment~\"Rule A\" and disabled=no)]\r\
    \n    enable [find where (comment~\"Rule B\" and disabled=yes)]\r\
    \n} else={\r\
    \n    disable [find where (comment~\"Rule B\" and disabled=no)]\r\
    \n    enable [find where (comment~\"Rule A\" and disabled=yes)]\r\
    \n}\r\
    \n" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=jan/01/1970 start-time=00:00:00

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 5:31 pm
by Amm0
[...] my problem is when the router is rebooted -(sometime every 14 hours [...]
In the "XY Problem" theme... while good to deal with the reboot case BUT if router reboots that frequently... that sounds like a bigger problem here ;)

And tend to agree with @rextended, keep it simple and just check the clock and enable/disable. Spread a task across multiple scripts gets confusing fast. Every "1m" may be aggressive but accurate e.g. if you log scripting, it would generate a fair number of log messages at 1m vs 1h

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 5:40 pm
by optio
[...] my problem is when the router is rebooted -(sometime every 14 hours [...]
In the "XY Problem" theme... while good to deal with the reboot case BUT if router reboots that frequently... that sounds like a bigger problem here ;)
And another... "so in the day 15 of the month my ISP-1 supscription is end and my router with no internet until i switch it manually.", how will these scheduled routes switching cover ended subscription at specific day?

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 5:42 pm
by rextended
It's actually very badly explained.
He probably exaggerates by writing that if he doesn't consume the traffic alternately, but all on one,
in the middle of the month he has already run out of traffic and has to switch manually to the second.
Anyway explained bad, it's true.

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 5:59 pm
by Amm0
Agreed, Think the underlying problem is there isn't an easy way to calculate the total data usage by period. And even/odd scheme is one way to deal with it.

If the problem is an internet has a data cap, perhaps just using one until it "didn't work" then switch to other ISP when that happens be another approach (e.g. via recursive route or netwatch script) .

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 7:11 pm
by Techsystem
can you explain more..?

Add to the rule "0" the comment "Rule A" (without quotes)
Add to the rule "2" the comment "Rule B" (without quotes)

Paste this on terminal.
/system scheduler
add interval=2d name=enable-routing-rule-a on-event="/routing rule\r\
    \nenable [find where comment~\"Rule A\"]\r\
    \ndisable [find where comment~\"Rule B\"]\r\
    \n" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=apr/01/2023 start-time=04:00:00
add interval=2d name=enable-routing-rule-b on-event="/routing rule\r\
    \nenable [find where comment~\"Rule B\"]\r\
    \ndisable [find where comment~\"Rule A\"]\r\
    \n" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=apr/02/2023 start-time=04:00:00
Done.

On reboot the routerboard keep same previous routing rule enabled, and everynight @04:00 (AM) switch the two rules.
Since the schedulers are on two different days, the rules are switched correctly.

Do you also need something that if the routerboard is off at 04:00 (AM) still syncs the right rule at the start without waiting for 04:00 the next day?
Hello Mr.rextended..!
thanks for your interrupt..!
realy thanksful..!!

Do you also need something that if the routerboard is off at 04:00 (AM) still syncs the right rule at the start without waiting for 04:00 the next day?

exactly that's what i want..

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 7:36 pm
by rextended
exactly that's what i want..

viewtopic.php?t=195272#p995554

Still:
Add to the rule "0" the comment "Rule A" (without quotes)
Add to the rule "2" the comment "Rule B" (without quotes)

Re: prevent schedule from restarting after reboot

Posted: Mon Apr 10, 2023 7:46 pm
by optio

Do you also need something that if the routerboard is off at 04:00 (AM) still syncs the right rule at the start without waiting for 04:00 the next day?

exactly that's what i want..

You have multiple options so far:
  • mkx proposal - multiple 2 day schedulers with 1 day start date shift to cover down period between 04:00 - 04:45 (or choose range that fits for you)
  • rextended proposal - single short 1m scheduler with odd/even day calculation
  • my proposal - each day toggle scheduler at some time (eg, 04:00) and recover startup scheduler like I mentioned earlyer: "There is also edge case if router is down at schedule time, to cover that, schedule which is executed at specific time of a day that is switching rules will need to set to some persistent variable of last execution time and addidional startup script will need to be created to check that persistent variable time and if is not in current day will need to execute switching rules...".
Don't ask how to create last one, don't have time :), try to learn from MT manuals and forum examples if you want like that.

Still you will have problem if subscription ends at some day, you will need to disable scheduler(s) until it is renewed or scheduler script will need to have some logic before switching to another ISP, eg. check if internet is reachable through its route if not revert back to old route.

Re: prevent schedule from restarting after reboot

Posted: Tue Apr 11, 2023 10:28 am
by Techsystem
Ok.... understand........

One unique scheduler, no matter when the routerboard is rebooted or for how many time is off.
No global variable, file or other frills needed. Just set the correct comments on Rule A and Rule B.

If the day is even, activate B, if it is odd, activate A.
Check constantly, change only if necessary.
/system scheduler
add interval=1m name=enable-daily-routing-rule on-event="/routing rule\r\
    \n:if (([:pick [/system clock get date] 4 6] % 2) = 0) do={\r\
    \n    disable [find where (comment~\"Rule A\" and disabled=no)]\r\
    \n    enable [find where (comment~\"Rule B\" and disabled=yes)]\r\
    \n} else={\r\
    \n    disable [find where (comment~\"Rule B\" and disabled=no)]\r\
    \n    enable [find where (comment~\"Rule A\" and disabled=yes)]\r\
    \n}\r\
    \n" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=jan/01/1970 start-time=00:00:00
really thanksful for everyone who shared his opinion/solution here in this thread
your script Mr @rextended is perfect for my situation really appreciate that..!!
i will accept it as a solution for my problem.
but in this last one do i have to add it to the previous one ..?
or apply this as a stand a lone schedule with no thong else with it..?
also what is the purpose of this line, what it mean..?
\n:if (([:pick [/system clock get date] 4 6] % 2) = 0) do={\r\

Re: prevent schedule from restarting after reboot

Posted: Tue Apr 11, 2023 11:22 am
by mkx
also what is the purpose of this line, what it mean..?
\n:if (([:pick [/system clock get date] 4 6] % 2) = 0) do={\r\

The solution is complete one ... so simply implement the scheduler command (and add comments on your routes because script by @rextended relies on them).

The menaing of quoted line is:
  • /system clock get date
    This command returns current date in format of mon/DD/YYYY ... If you run the command from command line, you won't see anything (you have to use :put [ ... ] construct to see the output). Today it returns apr/11/2023 ...
  • :pick [ ... ] 4 6
    This part cuts the argument (in this case the argument is whatever command within [ ... ] returns) ... it skips left most 4 characters and returns up to and including 6th character. As of today it'll return "11".
  • % 2
    Is operand which returns remainder after division (by 2).
  • if ( (...) = 0 )
    Runs following commands if argument (inside () ) equals 0. Today, the remainder of division is 1 (11 % 2 = 1), so today script would execute the second block of commands, which follow the else key word.
All in all: if day part of date is even (meaning that remainder when divided by 2 is 0), then ROS will execute first block of commands. Else it'll execute the second block of commands (which come after else key word).

Re: prevent schedule from restarting after reboot

Posted: Tue Apr 11, 2023 11:54 am
by rextended
@mkx,
nothing to add :)

Re: prevent schedule from restarting after reboot

Posted: Tue Apr 11, 2023 1:14 pm
by optio
Problem with this odd/even calculation is when end of month is odd (every 31st and on leap year 29th). It will not switch then on 1st next month (31 % 2 = 1 and 1 % 2 = 1).

Re: prevent schedule from restarting after reboot

Posted: Tue Apr 11, 2023 3:05 pm
by rextended
Problem with this odd/even calculation is when end of month is odd (every 31st and on leap year 29th). It will not switch then on 1st next month (31 % 2 = 1 and 1 % 2 = 1).
And so what?
New month, new calculations for limits, what happened last month doesn't matter.
Furthermore, if it is to be divided exactly between 31 days, the minutes in the month (if not even the seconds) must be counted and the time divided between the two connections.
Therefore, in the months of 30 and 28 days the exchange must be done every 24 hours, in the months of 31 days it must be done every 23h and ~14m,
And on bissextile Feb every 23h and ~10m......

I think you are now exaggerating.

Re: prevent schedule from restarting after reboot

Posted: Tue Apr 11, 2023 3:49 pm
by Techsystem
Problem with this odd/even calculation is when end of month is odd (every 31st and on leap year 29th). It will not switch then on 1st next month (31 % 2 = 1 and 1 % 2 = 1).
give or take one day its not a big issue..!

Re: prevent schedule from restarting after reboot

Posted: Tue Apr 11, 2023 6:18 pm
by optio
And so what?
New month, new calculations for limits, what happened last month doesn't matter.
Furthermore, if it is to be divided exactly between 31 days, the minutes in the month (if not even the seconds) must be counted and the time divided between the two connections.
Therefore, in the months of 30 and 28 days the exchange must be done every 24 hours, in the months of 31 days it must be done every 23h and ~14m,
And on bissextile Feb every 23h and ~10m......

I think you are now exaggerating.
I was just pointing out that route switch will not always happen every day if Techsystem wasn't aware of that, nothing else...

Re: prevent schedule from restarting after reboot

Posted: Tue Apr 11, 2023 6:32 pm
by Amm0
Problem with this odd/even calculation is when end of month is odd (every 31st and on leap year 29th). It will not switch then on 1st next month (31 % 2 = 1 and 1 % 2 = 1).
give or take one day its not a big issue..!
If that's the case, perhaps scheduler should could use less than 1m be okay too. If you need to enable logging on scripts, things that run really very often start cluttering the logs is only reason why I suggest.

Re: prevent schedule from restarting after reboot

Posted: Tue Apr 11, 2023 7:17 pm
by rextended
If that's the case, perhaps scheduler should could use less than 1m be okay too. If you need to enable logging on scripts, things that run really very often start cluttering the logs is only reason why I suggest.

Since the change happen only when the day change, you see only 2 logs everydays, no matter how often you exec the script.
The script do not uselessly enable what is already enabled and do not disable what is already disabled, so, no extra log.
The user should avoid adding the log unnecessarily where it is not needed.