Community discussions

MikroTik App
 
eldoncito2019
Member
Member
Topic Author
Posts: 332
Joined: Fri Jun 14, 2019 1:07 pm

Automate Script

Sat May 21, 2022 11:11 pm

Hello people, I have the following script which works very well but, what things are never missing a but, I have it in the script section and I have to put it to work manually every time my routerboard restarts, is there any way to put it to work automatically from scheduler or something every time I boot my routerboard. Thank you.
This is the script:
:global stopRouterRun false

:do {
             :local file "electric-cut.txt"
             :local data     [/system clock get date]
             :global arrMonths
             :local day      [:pick $data 4 6]
             :local mon     ($arrMonths->[:pick $data 0 3])
             :local year     [:pick $data 7 11]
             :local date "$day-$mon-$year"
             :local itime     [/system clock get time]
             :local hours   [:pick $itime 0 2]
             :local minsec [:pick $itime 2 8]
             :local mhours [:tonum $hours]
             :local msymbl "AM"
             :if ($mhours > 11) do={:set msymbl "PM"}
             :set mhours ($mhours % 12)
             :if ($mhours = 0) do={:set mhours 12}
             :if ($mhours < 10) do={:set mhours "0$mhours"}
             :local mtime "$mhours$minsec $msymbl"
             :local data1 "****************************\r\n      COURT:$date\r\n     AT: $mtime\r\n****************************"
             /file
             :if ([:len [find where name=$file]] = 0) do={print file=$file; :delay 5; set $file contents=""}
             set $file contents=$data1

             :delay 60
} while=(!$stopRouterRun)



EL DONCITO
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Automate Script

Sat May 21, 2022 11:14 pm

Is like you want schedule that at start-time=startup...
The solution is on the above line...

Your question is also the answer...
 
eldoncito2019
Member
Member
Topic Author
Posts: 332
Joined: Fri Jun 14, 2019 1:07 pm

Re: Automate Script

Sat May 21, 2022 11:17 pm

This script is to know at what time the routerboard stops working because there is a power outage, so I have to start it manually.







EL DONCITO
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Automate Script

Sat May 21, 2022 11:19 pm

I know what the script is for, I did it for you...

You named the scheduler yourself: use it

Is like you want schedule that at start-time=startup...
The solution is on the above line...

Your question is also the answer...
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Automate Script

Sat May 21, 2022 11:24 pm

Remember that if it starts automatically,
overwrites the file and you lost the "so precious" time ...
 
eldoncito2019
Member
Member
Topic Author
Posts: 332
Joined: Fri Jun 14, 2019 1:07 pm

Re: Automate Script

Sat May 21, 2022 11:31 pm

Remember that if it starts automatically,
overwrites the file and you lost the "so precious" time ...


You hit the nail on the head, I don't want to waste "so precious time", how would we do then?



EL DONCITO.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Automate Script

Sun May 22, 2022 12:53 am

Schedule this, not the script, directly write this inside scheduler text area.

The soluction is to create different file names each time routerboard starts.
:delay 10s

: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"}
:global stopRouterRun false

/system clock
:local data       [get date]
:local year       [:pick $data 7 11]
:local month      ($arrMonths->[:pick $data 0 3])
:local day        [:pick $data 4 6]
:local file       "electric-cut_$year-$month-$day_$[get time].txt"

:do {
    /system clock
    :local data   [get date]
    :local year   [:pick $data 7 11]
    :local month  ($arrMonths->[:pick $data 0 3])
    :local day    [:pick $data 4 6]
    :local date   "$day-$month-$year"
    :local itime  [get time]
    :local hours  [:pick $itime 0 2]
    :local minsec [:pick $itime 2 8]
    :local mhours [:tonum $hours]
    :local msymbl "AM"
    :if ($mhours > 11) do={:set msymbl "PM"}
    :set mhours ($mhours % 12)
    :if ($mhours =  0) do={:set mhours 12}
    :if ($mhours < 10) do={:set mhours "0$mhours"}
    :local mtime "$mhours:$minsec $msymbl"
    :local data1 "****************************\r\n      COURT:$date\r\n     AT: $mtime\r\n****************************"
    /file
    :if ([:len [find where name=$file]] = 0) do={print file=$file; :delay 5; set $file contents=""}
    set $file contents=$data1

    :delay 60s
} while=(!$stopRouterRun)
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3279
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Automate Script

Sun May 22, 2022 8:23 am

This scripts will write a file every minute to the flash drive. Would that shorten the flash life?

I would suggest you do monitor the router using an external program like splunk.
Here you can see many routers uptime and when they restarted. This is my garage router.
You can see uptime for 17 days and reboot 4 may (due to upgrade to 7.2.3)
.
uptime.png
And what user logged inn to router that day. But since I upgraded from 6.x that did not show all commands, I can only see some information.
.
Login.png
You do not have the required permissions to view the files attached to this post.
 
eldoncito2019
Member
Member
Topic Author
Posts: 332
Joined: Fri Jun 14, 2019 1:07 pm

Re: Automate Script

Mon May 23, 2022 4:20 pm

Friend REX, the script is working very well thanks for that, just one question: if I already have another script working very well and it has a global variable, how do I make that global variable that already exists in another script this new script use it and we do not have to add it again, I leave you the two scripts:
:global filenameIP "iphistory.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 data [/system clock get date]
             :local itime  [/system clock get time]
             :local hours  [:pick $itime 0 2]
             :local minsec [:pick $itime 2 8]
             :local mhours [:tonum $hours]
             :local msymbl "AM"
             :if ($mhours > 11) do={:set msymbl "PM"}
             :set mhours ($mhours % 12)
             :if ($mhours = 0) do={:set mhours 12}
             :if ($mhours < 10) do={:set mhours "0$mhours"}
             :local mtime "$mhours$minsec $msymbl"
:global now "HORA:$mtime   FECHA: $[:pick $data 4 6]-$($arrMonths->[:pick $data 0 3])-$[:pick $data 7 11]"
}

: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"}
:global stopRouterRun false

/system clock
:local data       [get date]
:local year       [:pick $data 7 11]
:local month      ($arrMonths->[:pick $data 0 3])
:local day        [:pick $data 4 6]
:local file       "electric-cut $day-$month-$year $[get time].txt"
I do not know if you understand me




EL DONCITO
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Automate Script

Mon May 23, 2022 5:00 pm

You don't have to care, as long as the two variables have the same content.
 
eldoncito2019
Member
Member
Topic Author
Posts: 332
Joined: Fri Jun 14, 2019 1:07 pm

Re: Automate Script

Mon May 23, 2022 5:22 pm

Ok, I understand, but if I want to use the variable that already exists in the first script, how would I do it in the second?



EL DONCITO.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Automate Script  [SOLVED]

Mon May 23, 2022 5:28 pm

On first:
:global savmevariablename <value.....>

On second and over just call it without add again value:
:global savmevariablename

But warning: everytime you must be sure than script 1 is everytime executed at least one time, before script 2
 
eldoncito2019
Member
Member
Topic Author
Posts: 332
Joined: Fri Jun 14, 2019 1:07 pm

Re: Automate Script

Mon May 23, 2022 6:00 pm

Very grateful for the explanation REX.



EL DONCITO

Who is online

Users browsing this forum: rextended and 26 guests