Community discussions

MikroTik App
 
imirsay
just joined
Topic Author
Posts: 21
Joined: Fri Jul 28, 2023 12:47 pm

if router uptime is more

Sun Jul 14, 2024 7:49 am

I need a script that executes commands if router uptime is more than one minute
Help me please
 
Apachez
Member Candidate
Member Candidate
Posts: 145
Joined: Mon Jul 01, 2024 11:45 pm

Re: if router uptime is more

Sun Jul 14, 2024 10:56 am

https://help.mikrotik.com/docs/display/ROS/Scheduler

Perhaps add it as a startup-script in the scheduler which will run it 3 seconds after boot but the first line in your script is something like:

delay 57s;

Then have the rest of your code which gives that the script starts 3 seconds after boot then sits and wait for 57 seconds before executing the rest of the script which gives that you have runned whatever you wish to run 1 minute after boot.
 
imirsay
just joined
Topic Author
Posts: 21
Joined: Fri Jul 28, 2023 12:47 pm

Re: if router uptime is more

Sun Jul 14, 2024 11:38 am

https://help.mikrotik.com/docs/display/ROS/Scheduler

Perhaps add it as a startup-script in the scheduler which will run it 3 seconds after boot but the first line in your script is something like:

delay 57s;

Then have the rest of your code which gives that the script starts 3 seconds after boot then sits and wait for 57 seconds before executing the rest of the script which gives that you have runned whatever you wish to run 1 minute after boot.



I need checked uptime. If the uptime was less, the command would not be executed
This command only has a delay
 
Apachez
Member Candidate
Member Candidate
Posts: 145
Joined: Mon Jul 01, 2024 11:45 pm

Re: if router uptime is more

Sun Jul 14, 2024 4:02 pm

Yes and delay of 3 seconds (once startup-script executes) + 57 seconds within the script before the rest of the script is being runned will give you that more than 1 minute has passed since the device got power on.

If you still refuse to take the easy way out you can pick the uptime variable and have it parsed from the output of "/system/resource/print".
 
Apachez
Member Candidate
Member Candidate
Posts: 145
Joined: Mon Jul 01, 2024 11:45 pm

Re: if router uptime is more

Mon Jul 15, 2024 12:50 am

Here is a workaround if you still want to figure out the seconds since boot but again using a "delay 60s;" as first row in your script is much more efficient along with schedule that script as startup-script.

:global myUPTIMESEC "0";
:global myTIMEOUT "60";
while ($myUPTIMESEC < $myTIMEOUT) do={
    :local tmpUPTIME [/system resource get uptime];
    :local tmpWEEKS [:pick $tmpUPTIME 0 [:find $tmpUPTIME "w"]];
    :set tmpUPTIME [:pick $tmpUPTIME ([:find $tmpUPTIME "w"]+1) [:len $tmpUPTIME]];
    :local tmpDAYS [:pick $tmpUPTIME 0 [:find $tmpUPTIME "d"]];
    :set tmpUPTIME [:pick $tmpUPTIME ([:find $tmpUPTIME "d"]+1) [:len $tmpUPTIME]];
    :local tmpHOURS [:pick $tmpUPTIME 0 [:find $tmpUPTIME ":"]];
    :set tmpUPTIME [:pick $tmpUPTIME ([:find $tmpUPTIME ":"]+1) [:len $tmpUPTIME]];
    :local tmpMINUTES [:pick $tmpUPTIME 0 [:find $tmpUPTIME ":"]];
    :set tmpUPTIME [:pick $tmpUPTIME ([:find $tmpUPTIME ":"]+1) [:len $tmpUPTIME]];
    :local tmpSECONDS $tmpUPTIME;
    :set myUPTIMESEC [(($tmpWEEKS*604800)+($tmpDAYS*86400)+($tmpHOURS*3600)+($tmpMINUTES*60)+$tmpSECONDS)];
#    :put ("Seconds since boot: ".$myUPTIMESEC);
    :delay 1s;
}

For the above the myTIMEOUT is what timeout you wish and you should probably adjust that ":delay 1s;" in the end if you dont need a second resolution (for example if you want to run your script after 1 hour its sufficient to change that delay into ":delay 1m;" or so).
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12333
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: if router uptime is more

Mon Jul 15, 2024 11:32 am

You do not specify RouterOS version. For v7 Simply...
{
    :local upsec [:tonum [/system resource get uptime]]
    :if ($upsec > 60) do={
        # ...script code...
    }
}
if the number is > 60... is more than 1 minutes that the device is up...


for v6, since is just a string...
{
    :if ([/system resource get uptime] > "00:01:00") do={
        # ...script code...
    }
}
 
Apachez
Member Candidate
Member Candidate
Posts: 145
Joined: Mon Jul 01, 2024 11:45 pm

Re: if router uptime is more

Mon Jul 15, 2024 11:41 am

You do not specify RouterOS version. For v7 Simply...
{
    :local ups [:tonum [/system resource get uptime]]
    :if ($ups > 60) do={
        # ...script code...
    }
}
if the number is > 60... is more than 1 minutes that the device is up...


for v6, since is just a string...
{
    :if ([/system resource get uptime] > "00:01:00") do={
        # ...script code...
    }
}

That was way shorter method in v7 compared to the manual one :-)

However you would need a while statement otherwise that if will just be runned once and "nope, its not been more than 60 seconds since boot" and nothing happens.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12333
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: if router uptime is more

Mon Jul 15, 2024 11:45 am

Please do not uselessly quote.

Mine was just a less bizarre example than what you wrote, to explain the concept.
The OP didn't explain like how you understand how the script should be done.
It doesn't say anywhere that it must loop until the condition occurs (or a scheduled task do the test afer x seconds),
but simply "executes commands if router uptime is more than one minute",
so, my script solution example "executes commands if router uptime is more than one minute".
 
Apachez
Member Candidate
Member Candidate
Posts: 145
Joined: Mon Jul 01, 2024 11:45 pm

Re: if router uptime is more

Mon Jul 15, 2024 12:05 pm

Without the loop your script will fail to execute if you read the OP's original intention to have the script being runned 1 minute after boot.

The proper solution is to add the script as a scheduled task to be runned at "startup" which by ROS occurs 3 seconds after boot completed and in that script you should have something like ":delay 57s;" (or just call it a day and have ":delay 60s;").

Having the loop and look for seconds since boot is more if you want to run the content multiple times but even then the proper solution is to set it as a scheduled task to be runned once every hour or such.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12333
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: if router uptime is more

Mon Jul 15, 2024 12:07 pm

What you write is absolutely not in the OP, what are you making up?

I need a script that executes commands if router uptime is more than one minute
Help me please

And on the other post that isa reply to your post:
I need checked uptime. If the uptime was less, the command would not be executed
This command only has a delay

I do not read at any point about "loop" or "script being runned 1 minute after boot" etc.

However, if it were really that, a wait of 57 seconds would simply be enough, as you have already written...
But he didn't like your answer, so I'm assuming that's NOT the case.

Of course, if the OP could explain better.................................
 
jaclaz
Forum Guru
Forum Guru
Posts: 1468
Joined: Tue Oct 03, 2023 4:21 pm

Re: if router uptime is more

Mon Jul 15, 2024 12:26 pm

I guess we are allowed to say that the OP's enumeration of required features, description of intended use, etc. is not particularly complete/exhaustive.

But, to be fair, he never asked for periodical running of the script.

Without further details, once the script is executed once (at startup +57s) with success (i.e. actually more than 1 minute passed), any subsequent runs after the first one will either:
1) always succeed as they will be executed at a time the router has been on for more than one minute
2) never be executed because the router has been rebooted and a new first instance (at boot-time + 57s) is the one running

On the other hand, if the script is scheduled to run at boot + 57s at every boot, it will always succeed anyway, or, if the router is rebooted any time t where 0<t<60, the delay won't allow the check to run.

The script could just print after the initial delay "More than 60 seconds passed since boot" without actually checking with /system resource get uptime, the check won't be run until the uptime is 60 seconds or more.
 
Apachez
Member Candidate
Member Candidate
Posts: 145
Joined: Mon Jul 01, 2024 11:45 pm

Re: if router uptime is more

Mon Jul 15, 2024 12:36 pm

What you write is absolutely not in the OP, what are you making up?

I need a script that executes commands if router uptime is more than one minute
Help me please

And on the other post that isa reply to your post:
I need checked uptime. If the uptime was less, the command would not be executed
This command only has a delay

I do not read at any point about "loop" or "script being runned 1 minute after boot" etc.

However, if it were really that, a wait of 57 seconds would simply be enough, as you have already written...
But he didn't like your answer, so I'm assuming that's NOT the case.

Of course, if the OP could explain better.................................

You really didnt read the OP (original post) did you?

Here is the usecase:

I need a script that executes commands if router uptime is more than one minute
 
holvoetn
Forum Guru
Forum Guru
Posts: 6050
Joined: Tue Apr 13, 2021 2:14 am
Location: Belgium

Re: if router uptime is more

Mon Jul 15, 2024 12:43 pm

Here is the usecase:
I need a script that executes commands if router uptime is more than one minute
Which is fulfilled when you run 3s after boot and first wait 57s.
Even regardless if time stamp on router has already been adjusted to reality.
So why that solution was dismissed, we can not know.

Yet he never said "check and wait until one minute has passed"

So it all boils down to incomplete / confusing requirements.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12333
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: if router uptime is more

Mon Jul 15, 2024 1:37 pm

You really didnt read the OP (original post) did you?

Here is the usecase:
I need a script that executes commands if router uptime is more than one minute

Mine is clearly more in line with the (incomplete) request.

So what?
No "loop" or "wait until" required.
Don't make things up.
He asked exactly "script that executes commands if router uptime is more than one minute" and there are no further details.

Don't have a limited view, the scripts are NOT only present in the scheduler,
but trivially it could also be something that must happen in the pppoe script or in the DHCP script (incomplete list)
that if for some reason the interface is not yet active, the execution fails .
And you can't start certain scripts late at startup because they are not interactive with parameters readable only by the pppoe or DHCP scripts.
 
Apachez
Member Candidate
Member Candidate
Posts: 145
Joined: Mon Jul 01, 2024 11:45 pm

Re: if router uptime is more

Mon Jul 15, 2024 2:40 pm

You really didnt read the OP (original post) did you?

Here is the usecase:


Mine is clearly more in line with the (incomplete) request.

So what?
No "loop" or "wait until" required.
Don't make things up.
He asked exactly "script that executes commands if router uptime is more than one minute" and there are no further details.

Don't have a limited view, the scripts are NOT only present in the scheduler,
but trivially it could also be something that must happen in the pppoe script or in the DHCP script (incomplete list)
that if for some reason the interface is not yet active, the execution fails .
And you can't start certain scripts late at startup because they are not interactive with parameters readable only by the pppoe or DHCP scripts.

Yes and your example with just an if-case will fail to fulfill that usecase. One need a while statement or run the script multiple times to get it to trigger when you use just an if-case in it which I pointed out with my post about using while-statement instead.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12333
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: if router uptime is more

Mon Jul 15, 2024 6:09 pm

Good thing you're there to foresee every use case, so your solution will be universal and everyone will praise you for providing it.
 
Apachez
Member Candidate
Member Candidate
Posts: 145
Joined: Mon Jul 01, 2024 11:45 pm

Re: if router uptime is more

Tue Jul 16, 2024 12:33 am

I fail to see why you are upset that somebody pointed out that your suggestion can be improved.

Doing just the if statement as your suggestion will have clear limitations of the script not working as expected. Compared to doing it as a whilte statement.

Most optimal is to NOT waste CPU resources and add a simple ":delay 60s;" at top of the script and call it a day.

Who is online

Users browsing this forum: No registered users and 7 guests