http://en.wikipedia.org/wiki/Cron
to start is needed PBOSL library - http://pbosl.purearea.net/ & Droopy Library http://gansta93.free.fr/droopyslib/
for information use - cron /help
CRON [/install] [/remove] [/stop] [/start] [/pause] [/resume]
[/edit] [/add] [/help]
[/install] - install cron service
[/remove] - remove cron service
[/stop] - stop cron service
[/start] - start cron service
[/pause] - pause cron service
[/resume] - resume cron service
[/edit] - edit cron table
[/add] line - add line with new command to cron table
[/help] - show this help
Code: Select all
;- Author : Petr Vavrin (peterb)
;- Location : Czech Republic
;- Email : pb.pb@centrum.cz
Enumeration
#file
EndEnumeration
PathToServiceEXE.s = GetProgramPath()
NameOfServiceEXE.s = GetProgramName()
FullPathToServiceEXE.s = PathToServiceEXE + NameOfServiceEXE
command.s = LCase(ProgramParameter())
Global path.s
path = GetSystemDirectory() + "\crontab"
Procedure$ set_col (symbol$, value$)
If symbol$ = "*"
ProcedureReturn value$
Else
ProcedureReturn symbol$
EndIf
EndProcedure
Procedure$ div_test (input.s, time.s)
If FindString(input, "/", 1)
If Val(time) % Val(StringField(input, 2, "/")) = 0
ProcedureReturn time
Else
ProcedureReturn input
EndIf
Else
ProcedureReturn input
EndIf
EndProcedure
Procedure$ interval_test (input.s)
out.s = ""
For c = 1 To CountString(input, ",") + 1
val.s = StringField(input, c, ",")
If FindString(val, "-", 1)
v_from = Val(StringField(val, 1, "-"))
v_to = Val(StringField(val, 2, "-"))
val = Str(v_from)
For x.l = v_from + 1 To v_to
val = val + "," + Str(x)
Next
EndIf
out.s + val + ","
Next
out = Mid(out, 1, Len(out) - 1)
ProcedureReturn out
EndProcedure
Procedure create_crontab()
If CreateFile(#file, path)
o$ = "# The Crontab File's Syntax" + #CRLF$
o$ + "#" + #CRLF$
o$ + "# minute hour day-of-month month-of-year day-of-week command" + #CRLF$
o$ + "#" + #CRLF$
o$ + "# Each of the above columns can be in one of the following formats (these examples are For the minute column): " + #CRLF$
o$ + "#" + #CRLF$
o$ + "# 30 - Run command at 30 minutes past the hour. " + #CRLF$
o$ + "# */10 - Run command once every 10 minutes, For the entire hour. " + #CRLF$
o$ + "# 15-30 - Run command once every minute, from 15 To 30 minutes past the hour. " + #CRLF$
o$ + "# 0,10,50 - Run command at 0 minutes past the hour, 10 minutes past the hour, And 50 minutes past the hour. " + #CRLF$
o$ + "# * - Run command once every minute. " + #CRLF$
o$ + "#" + #CRLF$
o$ + "# And here's the range of numbers available for each of the time and date columns: " + #CRLF$
o$ + "#" + #CRLF$
o$ + "# minute : 0-59" + #CRLF$
o$ + "# hour : 0-23" + #CRLF$
o$ + "# day-of-month : 0-31" + #CRLF$
o$ + "# month-of-year : 1-12" + #CRLF$
o$ + "# day-of-week : 0-6 (0=Sun, 1=Mon, 2=Tue, 3=Wed, 4=Thu, 5=Fri, 6=Sat" + #CRLF$
o$ + "#" + #CRLF$
o$ + "# sample line:" + #CRLF$
o$ + "# * * * * * c:\windows\notepad.exe" + #CRLF$
o$ + "#" + #CRLF$
WriteString(#file, o$)
CloseFile(#file)
EndIf
EndProcedure
Procedure cron()
now = Date()
n_minute.s = Str(Minute(now))
n_hour.s = Str(Hour(now))
n_dom.s = Str(Day(now))
n_month.s = Str(Month(now))
n_dow.s = Str(DayOfWeek(now))
n_year.s = Str(Year(now))
n_string.s = n_minute + " " + n_hour + " " + n_dom + " " + n_month + " " + n_dow
If ReadFile(#file, path)
While Eof(#file) = 0
line$ = Trim(ReadString(#file))
char$ = Mid(line$, 1, 1)
If (Asc(char$) = 42) Or (Asc(char$) > 47 And Asc(char$) < 58)
pos = FindString(line$, " ", 1)
minute$ = interval_test(Mid(line$, 1, pos - 1))
line$ = Trim(Mid(line$, pos + 1, Len(line$)))
pos = FindString(line$, " ", 1)
hour$ = interval_test(Mid(line$, 1, pos - 1))
line$ = Trim(Mid(line$, pos + 1, Len(line$) + 1))
pos = FindString(line$, " ", 1)
dom$ = interval_test(Mid(line$, 1, pos - 1))
line$ = Trim(Mid(line$, pos + 1, Len(line$) + 1))
pos = FindString(line$, " ", 1)
month$ = interval_test(Mid(line$, 1, pos - 1))
line$ = Trim(Mid(line$, pos + 1, Len(line$) + 1))
pos = FindString(line$, " ", 1)
dow$ = interval_test(Trim(Mid(line$, 1, pos - 1)))
command$ = Trim(Mid(line$, pos + 1, Len(line$) + 1))
For c1 = 1 To CountString(minute$, ",") + 1
v_minute.s = set_col(StringField(minute$, c1, ","), n_minute)
v_minute = div_test(v_minute, n_minute)
For c2 = 1 To CountString(hour$, ",") + 1
v_hour.s = set_col(StringField(hour$, c2, ","), n_hour)
v_hour = div_test(v_hour, n_hour)
For c3 = 1 To CountString(dom$, ",") + 1
v_dom.s = set_col(StringField(dom$, c3, ","), n_dom)
v_dom = div_test(v_dom, n_dom)
For c4 = 1 To CountString(month$, ",") + 1
v_month.s = set_col(StringField(month$, c4, ","), n_month)
v_month = div_test(v_month, n_month)
For c5 = 1 To CountString(dow$, ",") + 1
v_dow.s = set_col(StringField(dow$, c5, ","), n_dow)
v_dow = div_test(v_dow, n_dow)
out.s = v_minute + " " + v_hour + " " + v_dom + " " + v_month + " " + v_dow
If out = n_string And command$ <> ""
RunProgramEx(command$, 1)
EndIf
Next
Next
Next
Next
Next
EndIf
Wend
CloseFile(#file)
Else
create_crontab()
EndIf
EndProcedure
Procedure MyServiceFunction()
now = Date()
If Second(now) = 0
cron()
Delay(2000)
EndIf
Delay(300)
MyServiceFunction()
EndProcedure
Procedure NTNotify(Parameter)
Select Parameter
Case 2
MessageRequester("Info","Service Cron is paused",0) ;
Case 3
MessageRequester("Info","Service Cron is resumed",0);
EndSelect
EndProcedure
If command = "/?" Or command = "/h" Or command = "help" Or command = "/help"
command = "/help"
EndIf
ServiceName.s = "Cron"
ServiceDisplayName.s = "Cron"
ServiceExplanation.s = "Execute commands or scripts automatically at a specified time/date"
Select command
Case "/install"
; start types
;#SERVICE_BOOT_START = $00000000
;#SERVICE_SYSTEM_START = $00000001
;#SERVICE_AUTO_START = $00000002
;#SERVICE_DEMAND_START = $00000003
;#SERVICE_DISABLED = $00000004
; Create the Service an run....
Installservice(ServiceName, ServiceDisplayName, FullPathToServiceEXE, ServiceExplanation, $00000002)
End
Case "/remove"
RemoveService(ServiceName)
End
Case "/stop"
StopService(ServiceName)
End
Case "/pause"
PauseService(ServiceName)
End
Case "/start"
StartService(ServiceName)
End
Case "/resume"
ResumeService(ServiceName)
End
Case "/edit"
If ReadFile(#file, path)
CloseFile(#file)
RunProgramEx("notepad.exe " + path, 0)
Else
create_crontab()
EndIf
End
Case "/add"
cmd$ = GetFullCmdArgs()
cmd$ = Mid(cmd$, FindString(cmd$, "/add", 1) + 5, Len(cmd$))
If OpenFile(#file, path)
FileSeek(#file, Lof(#file))
WriteStringN(#file, cmd$)
CloseFile(#file)
EndIf
End
Case "/help"
If OpenConsole()
PrintN("CRON [/install] [/remove] [/stop] [/start] [/pause] [/resume]")
PrintN(" [/edit] [/add] [/help]")
PrintN("")
PrintN("[/install] - install cron service")
PrintN("[/remove] - remove cron service")
PrintN("[/stop] - stop cron service")
PrintN("[/start] - start cron service")
PrintN("[/pause] - pause cron service")
PrintN("[/resume] - resume cron service")
PrintN("[/edit] - edit cron table")
PrintN("[/add] line - add line with new command to cron table")
PrintN("[/help] - show this help")
CloseConsole()
EndIf
End
EndSelect
Service(ServiceName, @MyServiceFunction(),@NTNotify())