A little help please

I have the following script and it works very well for me, every time the electricity is cut off it tells me in a file.txt the date and time the service was cut off.

:global stopRouterRun false;

:do {
	:local file "electric cut.txt";
	:local day ([:pick [/system clock get date] 4 6]);
	:local mon ([:pick [/system clock get date] 0 3]);
	:local year ([:pick [/system clock get date] 7 11]);
	:local date "$day-$mon-$year";
	:local time (:put [/system clock get time]);
	:local data1 ("  court: " . $date . " at: " . $time .  " ) ;
	/file print file="$file"
	/file set $file contents=$data1;

	:delay 60;
} while=(!$stopRouterRun)

I would like you to help me with a script that records in a .txt file, the time when the electrical service was restored,
but it should be taken into account that every time the electricity arrives, the internet does not arrive at the same time,
I do not know if you understand me Thanks for your help, especially Rextended and Jotne.


EL DONCITO.

Have you tryed

//system scheduler add name="create _file" on-event=<your script to generate file> policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-time=startup

?

startup - execute the script 3 seconds after the system startup.

(from https://wiki.mikrotik.com/wiki/Manual:System/Scheduler)

PS: please reduce the allowed policies to what is needed.

It doesn’t work friend

EL DONCITO.

Is clear that @afuchs do not read your OP…


but you do not have history if the power go away more than one time…

add

:delay 60s

at start of the script

you can add more seconds, for the time needed to the connection go up…

Thanks for assisting me Rextended, but I need a script that creates a file.txt for me to know at what time the electrical service was restored in my RB, I hope you understand me.

EL DONCITO.

… a script that creates a file.txt for me to know at what time the electrical service was restored in my RB …

The routerboard need internet to setup correctly the clock, the 120s waiting is for waiting the Internet and time sync

Shedule the script to start at startup.

Keeping values of each boot

:delay 120s
:global shutname "shutdown.txt"
:global arrMonths {jan="01";feb="02";mar="03";apr="04";may="05";jun="06";jul="07";aug="08";sep="09";oct="10";nov="11";dec="12"}
{
:local ndate [/system clock get date]
:local now "$[:pick $ndate 7 11]-$($arrMonths->[:pick $ndate 0 3])-$[:pick $ndate 4 6] $[/system clock get time]"
/file
:if ([:len [find where name="flash" and type="disk"]] = 1) do={:set shutname "flash/$shutname"}
:if ([:len [find where name=$shutname]] = 0) do={print file=$shutname; :delay 5s; set $shutname contents=""}
:delay 5s
:local filecontent [get $shutname contents]
:local newfilecontent "$now\r\n$filecontent"
set $shutname contents=$newfilecontent
}

Only the last

:delay 120s
:global shutname "shutdown.txt"
:global arrMonths {jan="01";feb="02";mar="03";apr="04";may="05";jun="06";jul="07";aug="08";sep="09";oct="10";nov="11";dec="12"}
{
:local ndate [/system clock get date]
:local now "$[:pick $ndate 7 11]-$($arrMonths->[:pick $ndate 0 3])-$[:pick $ndate 4 6] $[/system clock get time]"
/file
:if ([:len [find where name="flash" and type="disk"]] = 1) do={:set shutname "flash/$shutname"}
:if ([:len [find where name=$shutname]] = 0) do={print file=$shutname; :delay 5s; set $shutname contents=""}
:delay 5s
set $shutname contents=$now
}

If you notice… everytime is the same script…

Rextended, the script is overwriting the file, what I want is for it to tell me at what time the electricity was restored, and from there it stops, Wouldn't it be better to do a subtraction of the hour minus the uptime, I don't know.
Do you understand me?

EL DONCITO.

the script keep history of shutdown (every time the script is launched…)
the file is never deleted, last bootup is added on top
do some operations, like subtract minutes, is useless, everytime the routerboard is started 120s + 3s before the date inside the script.
Why do not correct it?
For example 00:00 - 2min and 3 sec = -23:57:57, not the date of previous day, but a negative time value…

I want is for me to register, the last start of the RB and not to be overwriting the file. Thank you for your valuable collaboration.



EL DONCITO.

Ok, this create single files:

:delay 120s
:global arrMonths {jan="01";feb="02";mar="03";apr="04";may="05";jun="06";jul="07";aug="08";sep="09";oct="10";nov="11";dec="12"}
{
:local ndate [/system clock get date]
:local now "$[:pick $ndate 7 11]-$($arrMonths->[:pick $ndate 0 3])-$[:pick $ndate 4 6]_$[/system clock get time]"
:local shutname "booted_$now.txt"
/file
:if ([:len [find where name="flash" and type="disk"]] = 1) do={:set shutname "flash/$shutname"}
:if ([:len [find where name=$shutname]] = 0) do={print file=$shutname; :delay 5s; set $shutname contents=""}
}

Every time the router is powered (and after 120s for make internet available and time sincronized)
a file name booted_2022-04-06_19:42:23.txt is created.
do not deplete the space because the file is empty. (really use ~64byte for each filename)

Now it is creating many files, Rextended.
What I am saying is that for example we have the next time 14:10:00, and the uptime is 01:03:00, make a file for me and say that the equipment booted 13:07:00,
you understand me friend.

EL DONCITO.

You can use the “Only the last” script at post #6:
http://forum.mikrotik.com/t/a-little-help-please/157117/6

Why continuosly run the script?
The boot time is still the same…
Is useless every minute do the calc and write it on file, the boot time is the exact same value…
the equipment booted everytime at 13:07:00

Is better you explain what you want do at the end, instead of ask about intermediate process, because often I do not understand you…

I want to make a script that writes me a file.txt, where it says at what time the RB booted and leaves it like that, but we have to take into account that the internet does not arrive all at once, that's why I say that it is better to execute it every 10 minutes , you understand me friend Rextended.


EL DONCITO.

Still the same, as long as the routerboard is not powered, it is useless to recalculate and rewrite the file…

Also, if routerboard do not have time sincronized, is useless do any calc, because can not provide any usable values.

Let me think for some seconds …

ok… I write one script…
just the time to fix it…

This script must be launched at startup.

:global arrMonths {jan="01";feb="02";mar="03";apr="04";may="05";jun="06";jul="07";aug="08";sep="09";oct="10";nov="11";dec="12"}
{
:local filename "firsttimesync.txt"
/file
:if ([:len [find where name="flash" and type="disk"]] = 1) do={:set filename "flash/$filename"}
:if ([:len [find where name=$filename]] = 0) do={print file=$filename}
:delay 5s
set $filename contents="undefined"
:do { :delay 1s } while=([:typeof [/system ntp client get last-update-before]] != "time")
:local ndate [/system clock get date]
:local now "$[:pick $ndate 7 11]-$($arrMonths->[:pick $ndate 0 3])-$[:pick $ndate 4 6] $[/system clock get time]"
set $filename contents=$now
}

What do: at startup replace previous datetime with “undefined”, then wait until time is syncronized for first time from routerboard boot,
and write inside the file firsttimesync.txt the date and time of that moment.
Any more execution is useless.

Subtracting uptime from that datetime for obtain the precise date and time of boot is another thing…

and using this:

:global time [/system clock get time];
:local uptime [/system resource get uptime];

EL DONCITO.

On RouterOS the variable of type datetime do not exist…

:put [:typeof [/sys clock get date]]
:put [:typeof [/sys clock get time]]
:put [:typeof [/system resource get uptime]]

the returned value for date are simply a string…
and a time for time and uptime…

working with string is like to do

:put ("useless" - "nothing")

If the time is 21:04 and your subtract uptime, for example 2 days, you simply obtain
-1d02:55:31

And you must still calculate manually the value…

With my script you have a great approximation when the RouterBOARD is for the first time online after (re)boot
And the value is close to last boot datetime

So do you think the script is fine, if it writes in the file the time the routerboard booted



EL DONCITO