Difference between revisions of "Crontab"

From OpenKM Documentation
Jump to: navigation, search
(BeanShell sample)
(JAR sample)
Line 102: Line 102:
  
 
     for (int i=0; i<5; i++) {
 
     for (int i=0; i<5; i++) {
       sb.append(i).append("<hr/>");
+
       sb.append(i).append("<br/>");
 
     }
 
     }
  

Revision as of 22:16, 15 September 2011

With this feature you can create scheduled task in a easy way. In this page you can see the already registered tasks. The last start and last stop columns show when the last time a task was executed. If you want to the the output of the script, you can set an email and you will be notified every time the task is executed.


Okm admin crontab 01.png

Nota idea.png If you want to test an execution, click on the execute action (flash icon) and this will force the execution. If you have specified an email address, this email also will be sent.


Nota clasica.png Starting with OpenKM 5.1.2 the field "Type" was removed and will be autodetected from the uploaded file.

When you create or modify a scheduled task, the last start and last stop entries reseted if you upload a new task definition. Script may be BeanShell (for little amount of code) or a packet Jar (for more complex logic or bigger scripts). The daemon will look for the Main-Class attribute in the Jar's META-INF/MANIFEST.MF.


Okm admin crontab 02.png

Nota advertencia.png Only active tasks will be executed periodically. If you don't want a task to be execute, simple mark it as inactive or remove it.

Expression syntax

Commands are executed by cron when the minute, hour, and month of year fields match the current time, and when at least one of the two day fields (day of month, or day of week) match the current time. The scheduler examines crontab entries once every minute. The time and date fields are:


Field Allowed values
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)


A field may be an asterisk (*), which always stands for first-last.

Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive. For example, "8-11" for an hours entry specifies execution at hours 8, 9, 10 and 11.

Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples: ""1,2,5,9", "0-4,8-12".

Step values can be used in conjunction with ranges. Following a range with "/<number>" specifies skips of the number's value through the range. For example, "0-23/2" can be used in the hours field to specify command execution every other hour. Steps are also permitted after an asterisk, so if you want to say "every two hours", just use "*/2".

Names can also be used for the month and day of week fields. Use the first three letters of the particular day or month (case doesn't matter). Ranges or lists of names are not allowed.


Nota clasica.png The day of a command's execution can be specified by two fields: day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time. For example, "30 4 1,15 * 5" would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.

Instead of the first five fields, one of eight special strings may appear:


String Meaning
@yearly Run once a year, "0 0 1 1 *"
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *"
@weekly Run once a week, "0 0 * * 0"
@daily Run once a day, "0 0 * * *"
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *"


BeanShell sample

Create a file called beanshellSample.bsh with this content:

for (int i=0; i<5; i++) {
  print("Iteration: " + i + "<br/>);
}

Now go to Administration > Crontab and create an entry with this input:

  • Name: BeanShell Sample
  • Mail: yourmail@domain.com
  • Expression: */5 * * * *
  • Active: On

This will create a crontab job which execute the contents of the beanshellSample.bsh file every 5 minutes. The output will be sent to the defined email address.

JAR sample

Create a Java project with a class called JarSample.java" with this content:

package com.openkm.sample;

public class JarSample {
	
  public static void main(String[] args) {
    System.out.println(cronTask());
  }

  public static String cronTask() {
    StringBuilder sb = new StringBuilder();

    for (int i=0; i<5; i++) {
      sb.append(i).append("<br/>");
    }

    return sb.toString();
  }

Now create the JAR. From Eclipse can be done from the contextual menu Export... and select Java > JAR file. Follow the wizard selecting the previously created class (com.openkm.sample.JarSample) as main class in the MANIFEST.MF.

Go to Administration > Crontab and create an entry with this input:

  • Name: JAR Sample
  • Mail: yourmail@domain.com
  • Expression: */5 * * * *
  • Active: On

This will create a crontab job which execute the contents of the JarSample.jar file every 5 minutes. The output will be sent to the defined email address.