Page 1 of 1

Cron

Posted: Sun Feb 18, 2007 3:00 am
by peterb
Cron for windows

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()) 


Cron for windows

Posted: Sun Feb 18, 2007 1:56 pm
by Frank Smart
I didn't manage to get it compiled... :cry:

But after some changes... Didn't you mention that you use Droopy's Lib?

Code: Select all


diff Bi C:/cron1.pb C:/cron.pb
8a9,94
> ;{ Start From Droopy's Lib
> 
> Procedure.s GetProgramPath()
>   ProgramName.s=Space(255)
>   GetModuleFileName_(0,@ProgramName,255) 
>   ProcedureReturn GetPathPart(ProgramName)
> EndProcedure
> 
> 
> Procedure.s GetProgramName()
>   ProgramName.s=Space(255)
>   GetModuleFileName_(0,@ProgramName,255) 
>   ProcedureReturn GetFilePart(ProgramName)
> EndProcedure
> 
> Procedure.s GetSystemDirectory()
>   Path.s=Space(500)
>   GetSystemDirectory_(@Path,500)
>   ProcedureReturn Path
> EndProcedure
> 
> Declare RunProgramEx2(ProgramNameAndParameters.s,flags)
> 
> Procedure RunProgramEx(ProgramNameAndParameters.s)
>   ProcedureReturn RunProgramEx2(ProgramNameAndParameters,0)
> EndProcedure
> 
> Procedure IsThreadRunning(ThreadID)
>   GetExitCodeThread_(ThreadID, @ExitCode.l) 
>   
>   If ExitCode = #STATUS_PENDING 
>     retour=1 ; the thread is still running 
>   Else 
>     retour=0 ; the thread has quit 
>   EndIf
>   
>   ProcedureReturn retour
>   
> EndProcedure
> 
> Procedure RunProgramEx2(ProgramNameAndParameters.s,flags)
>   si.STARTUPINFO 
>   pi.PROCESS_INFORMATION 
>   si\cb = SizeOf(STARTUPINFO) 
>   si\dwFlags=#STARTF_USESHOWWINDOW
>   
>   ;/ Si Hide
>   If flags & 2
>     si\wShowWindow=#SW_HIDE
>   Else
>     si\wShowWindow=#SW_NORMAL
>   EndIf
>   
>   retour=CreateProcess_(#Null,ProgramNameAndParameters,#Null,#Null,#False,0,#Null,#Null,@si, @pi) 
>   
>   If retour ;/ Succès de l'exécution
>     
>     ;/ Si il faut attendre que l'appli soit terminée
>     ;/ On attends que le thread soit terminé
>     If flags & 1
>       Thread = pi\hThread
>       While IsThreadRunning(Thread)
>         Delay(1)
>       Wend
>     EndIf
>     
>     ProcedureReturn pi\hProcess ;/ On renvoie le Handle du Process créé
>   EndIf
>   
>   ;/ Erreur d'exécution --> On renvoie 0
>   
> EndProcedure
> 
> Procedure.s GetFullCMDLine(); get the full command line sent to the app - please see help file!
>   ProcedureReturn PeekS(GetCommandLine_())
> EndProcedure
> 
> Procedure.s GetFullCmdArgs() ; get all of the command line arguments passed to your program - see the help file
>   cmdline.s = GetFullCMDLine()
>   cmdline = Right(cmdline, Len(cmdline)-1)
>   cmdline = Right(cmdline, Len(cmdline) - (FindString(cmdline, Chr(34), 0) +1))
>   ProcedureReturn cmdline
> EndProcedure
> 
> ;} End from Droopy's lib
> 
155c241
<                     RunProgramEx(command$, 1) 
---
>                     RunProgramEx2(command$, 1) 
249c335
<       RunProgramEx("notepad.exe " + path, 0) 
---
>       RunProgramEx2("notepad.exe " + path, 0) 

Cheers Frank

Posted: Sun Feb 18, 2007 2:07 pm
by peterb
I'm sorry, I used so Droopy's Lib

Cron for windows

Posted: Sun Feb 18, 2007 2:47 pm
by Frank Smart
:D Thank you for sharing this :D

I only was a bit irritaded that it didn't even compile.

I am glad to find a Cron-Clone for win32. You did a great job. A big thankyou!

Cheers Frank