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
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 problemsWhy would you want to do that ?
Use NTP client.
Or IP / Cloud.
Much easier and more accurate.
sorry i can not understandYou will wear out the flash doing this.
How do you know the date is right when you save the file?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
No, it does check frequently to update time.ntp update date and time only one time,
That's true but how do you know the time you write back is correct ? You don't...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
:global $SaveNTP; $SaveNTP YourMonitorScriptName
# 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;
};
};
:local fileName "$scriptName.txt";
...
:set fileMessage "routerName=$routerName\r\nuptime=$uptime\r\ndate=$date\r\ntime=$time"
# 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;
};
# 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;
};
It is better to run time-sensitive scripts after checking the NTP status: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...
/system/ntp/client/get status as-string
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...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