Community discussions

MikroTik App
 
abdalgalil
just joined
Topic Author
Posts: 13
Joined: Sun Jul 17, 2022 2:53 pm

script to save current date and time in text file

Thu May 23, 2024 6:13 pm

Hello
i want script to save current date and time in text file and another script to restore date and time from this text file after reboot
 
User avatar
patrikg
Member
Member
Posts: 362
Joined: Thu Feb 07, 2013 6:38 pm
Location: Stockholm, Sweden

Re: script to save current date and time in text file

Thu May 23, 2024 6:19 pm

You will wear out the flash doing this.
 
holvoetn
Forum Guru
Forum Guru
Posts: 6753
Joined: Tue Apr 13, 2021 2:14 am
Location: Belgium

Re: script to save current date and time in text file

Thu May 23, 2024 8:24 pm

Why would you want to do that ?
Use NTP client.
Or IP / Cloud.

Much easier and more accurate.
 
abdalgalil
just joined
Topic Author
Posts: 13
Joined: Sun Jul 17, 2022 2:53 pm

Re: script to save current date and time in text file

Fri May 24, 2024 1:15 am

Why would you want to do that ?
Use NTP client.
Or IP / Cloud.

Much easier and more accurate.
ntp update date and time only one time, and save this value if router reboot then start with this saved value .. if not found internet as my case to update the time it will running with this wrong saved time and make many problems
so i want to save always currect time
 
abdalgalil
just joined
Topic Author
Posts: 13
Joined: Sun Jul 17, 2022 2:53 pm

Re: script to save current date and time in text file

Fri May 24, 2024 1:17 am

You will wear out the flash doing this.
sorry i can not understand
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: script to save current date and time in text file

Fri May 24, 2024 10:43 am

Hello
i want script to save current date and time in text file and another script to restore date and time from this text file after reboot
How do you know the date is right when you save the file?

The fact that you create the file already assigns it the current date and time,
so in recovery just read the date and time of the file,
without worrying about reading or writing the content.

The most obvious thing is to let the NTP client do its job.
 
holvoetn
Forum Guru
Forum Guru
Posts: 6753
Joined: Tue Apr 13, 2021 2:14 am
Location: Belgium

Re: script to save current date and time in text file

Fri May 24, 2024 10:52 am

ntp update date and time only one time,
No, it does check frequently to update time.
That's the whole point of using NTP process.
if not found internet as my case to update the time it will running with this wrong saved time and make many problems
so i want to save always currect time
That's true but how do you know the time you write back is correct ? You don't...
Besides, if you have no internet, I think you have other problems then only wrong timestamps.
 
UkRainUa
newbie
Posts: 39
Joined: Sun Mar 10, 2024 3:10 am

Re: script to save current date and time in text file

Sat May 25, 2024 3:32 pm

Hi.
You can run this on startup and then call it as a function after some NTP events.
It is possible to use a log parser to analyze NTP messages.
So, you need a script that will monitor NTP events and call this one :)
:global $SaveNTP; $SaveNTP YourMonitorScriptName 
This creates a csv file "SaveNTP.csv" "YourMonitorScriptName" as argument.
You will see an error messages "YourMonitorScriptName SaveNTP ...." in the system log if something wrong.
# SaveNTP function
:global SaveNTP do={

:local scriptName "SaveNTP";
:local fileName "$scriptName.csv";
:local fileMessage "";
:local errorMessage "";
/log debug "$1 $scriptName started";

# file add
do {
	:if ([/file find name=$fileName]) do={} else={/file add name=$fileName};
} on-error={
	:set errorMessage "$1 $scriptName file add error";
	/log warning $errorMessage;
	:return false;
};

# message
:do {
	:local routerName [/system identity get name];
	:local uptime [/system resource get uptime];
	:local date [/system clock get date];
	:local time [/system clock get time];
	:set fileMessage "$routerName;$uptime;$date;$time"
} on-error={
	:set errorMessage "$1 $scriptName message error";
	/log warning $errorMessage;
	:return false;
};

# file set
:do {
	:if ([/file find name=$fileName]) do={
		/file set $fileName contents=$fileMessage;
	} else={
		:set errorMessage "$scriptName file find error";
		/log warning $errorMessage;
		:return false;
	};
	/log debug "$scriptName executed";
	:return true;
} on-error={
	:set errorMessage "$scriptName file set error";
	/log warning $errorMessage;
	:return false;
};

};
Another idea is to store the data not with a separator, but with the name of the variable for import: viewtopic.php?t=38191
:local fileName "$scriptName.txt";
...
:set fileMessage "routerName=$routerName\r\nuptime=$uptime\r\ndate=$date\r\ntime=$time"

PS
Since you'll need a system log parser or other NTP event monitor, just don't do anything that requires accurate time. Until the time is synchronized. Why do you need an outdated file? :)
 
UkRainUa
newbie
Posts: 39
Joined: Sun Mar 10, 2024 3:10 am

Re: script to save current date and time in text file

Sun May 26, 2024 3:33 pm

# SaveNTP

:local scriptName "SaveNTP";
:local fileName "$scriptName.csv";
:local fileMessage [/system clock get];
:local errorMessage "";
/log debug "$scriptName started";

# file add
do {
	:if ([/file find name=$fileName]) do={} else={/file add name=$fileName};
} on-error={
	:set errorMessage "$scriptName file add error";
	/log warning $errorMessage;
	:return false;
};

:delay 1s;
	
# file set
:do {
	:if ([/file find name=$fileName]) do={
		/file set $fileName contents=$fileMessage;
	} else={
		:set errorMessage "$scriptName file find error";
		/log warning $errorMessage;
		:return false;
	};
	/log debug "$scriptName executed";
	:return true;
} on-error={
	:set errorMessage "$scriptName file set error";
	/log warning $errorMessage;
	:return false;
};
Last edited by UkRainUa on Sun May 26, 2024 9:28 pm, edited 1 time in total.
 
UkRainUa
newbie
Posts: 39
Joined: Sun Mar 10, 2024 3:10 am

Re: script to save current date and time in text file

Sun May 26, 2024 3:38 pm

# LoadNTP

:local scriptName "LoadNTP";
:local fileName "SaveNTP.csv";
:local errorMessage "";
:local fileContents "";

/log debug "$scriptName started";

# LoadValue function
: local LoadValue do={
		:local start [:find $1 "$2="];
		:set start ($start + [:len $2] + 1);
		:local end [:find from=$start $1 ";"];
		:return [:pick $1 $start $end];
};

# file contents
:do {
	:if ([/file find name=$fileName]) do={
		:set fileContents [/file get [/file find name=$fileName] contents];
	} else={
		:set errorMessage "$scriptName file find error";
		/log warning $errorMessage;
		:return false;
	};
} on-error={
	:set errorMessage "$scriptName file contents error";
	/log warning $errorMessage;
	:return false;
};

# date time set
:do {
	/system/clock/set date=[$LoadValue $fileContents date];
	/system/clock/set time=[:totime [$LoadValue $fileContents ";time"]];
	/log debug "$scriptName executed";
	:return true;
} on-error={
	:set errorMessage "$scriptName date time set error";
	/log warning $errorMessage;
	:return false;
};
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: script to save current date and time in text file

Mon May 27, 2024 1:47 am

too many useless steps...
as I already said, just create the file, and then re-read the time and date of the file,
without wasting time writing and reading inside the file...
 
UkRainUa
newbie
Posts: 39
Joined: Sun Mar 10, 2024 3:10 am

Re: script to save current date and time in text file

Tue May 28, 2024 7:05 pm

too many useless steps...
as I already said, just create the file, and then re-read the time and date of the file,
without wasting time writing and reading inside the file...
It is better to run time-sensitive scripts after checking the NTP status:
 /system/ntp/client/get status as-string
 
optio
Forum Veteran
Forum Veteran
Posts: 945
Joined: Mon Dec 26, 2022 2:57 pm

Re: script to save current date and time in text file

Tue May 28, 2024 8:53 pm

ntp update date and time only one time, and save this value if router reboot then start with this saved value .. if not found internet as my case to update the time it will running with this wrong saved time and make many problems
so i want to save always currect time
ROS saves current time on user initiated reboot or shutdown, unless is power outage. In such case not sure how practical is to restore some interval saved time since it will be wrong anyway because you don't know how long device was off. If this is because of some PKI then you will need to setup NTP sync over LAN or WAN to ensure that time drifts cannot cause issues with certificate validation. If you cannot setup NTP for some reason on your network, use at least UPS to avoid power outages and manually correct time drift occasionally...
 
holvoetn
Forum Guru
Forum Guru
Posts: 6753
Joined: Tue Apr 13, 2021 2:14 am
Location: Belgium

Re: script to save current date and time in text file

Tue May 28, 2024 11:20 pm

As already said, there are other more urgent problems then timestamps if there is no network connection...
 
abdalgalil
just joined
Topic Author
Posts: 13
Joined: Sun Jul 17, 2022 2:53 pm

Re: script to save current date and time in text file  [SOLVED]

Tue Sep 03, 2024 7:08 pm

thank you for your replying
here is the easiest way to do this
save the time and date in text file time.txt and put it in the root directory interval 10m for example:
/system scheduler add name=save-time start-time=startup interval=10m on-event=":delay 60s\r\
\n:local currentTime [/system clock get time]\r\
\n:local currentDate [/system clock get date]\r\
\n/file print file=time.txt where name=\"time.txt\"\r\
\n/file set time.txt contents=\"\$currentDate \$currentTime\""

and restore it by using this:
/system scheduler add name=restore-time start-time=startup on-event=":local savedTime [/file get time.txt contents]\r\
\n:local savedDate [:pick \$savedTime 0 10]\r\
\n:local savedClock [:pick \$savedTime 11 19]\r\
\n/system clock set date=\$savedDate time=\$savedClock"

note:
if you use another time format like v6 (jul/02/2024) you need to change the pick value to 0 11 and 12 20

Who is online

Users browsing this forum: No registered users and 9 guests