Difference between revisions of "Crontab"

From OpenKM Documentation
Jump to: navigation, search
m (JAR sample)
(JAR sample)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
{{TOCright}} __TOC__
 +
 
With this feature you can create scheduled tasks in a easy way. In this page you can see the already registered tasks. The ''last start'' and ''last stop'' columns show the last time a task was executed. If you want to see the output of the script, you can set an email and you will be notified every time the task is executed.
 
With this feature you can create scheduled tasks in a easy way. In this page you can see the already registered tasks. The ''last start'' and ''last stop'' columns show the last time a task was executed. If you want to see the output of the script, you can set an email and you will be notified every time the task is executed.
  
Line 111: Line 113:
  
 
{{Note|The crontab daemon will look for a static method called '''cronTask''' with no arguments to be executed. The string returned by this method will be included in the crontab execution result email.}}
 
{{Note|The crontab daemon will look for a static method called '''cronTask''' with no arguments to be executed. The string returned by this method will be included in the crontab execution result email.}}
 +
 +
{{Warning|Since OpenKM 6.4 this '''cronTask''' method has an String argument with the system token.}}
  
 
Now create the JAR. From Eclipse you can use the contextual menu '''Export...''' and select '''Java''' > '''JAR file'''. Follow the wizard selecting the previously created class (com.openkm.sample.JarSample) as the main class in the MANIFEST.MF.
 
Now create the JAR. From Eclipse you can use the contextual menu '''Export...''' and select '''Java''' > '''JAR file'''. Follow the wizard selecting the previously created class (com.openkm.sample.JarSample) as the main class in the MANIFEST.MF.
Line 122: Line 126:
  
 
This will create a crontab job which will execute the contents of the ''JarSample.jar'' file every 5 minutes. The output will be sent to the defined email address.
 
This will create a crontab job which will execute the contents of the ''JarSample.jar'' file every 5 minutes. The output will be sent to the defined email address.
 +
 +
== Users examples ==
 +
===  Network importing ===
 +
The scanner saves scans as pdfs to a shared folder ('/home/scanner') on the server then this script picks them up, loads them into an OpenKM folder ('/okm:root/Scans') and deletes the originals. I am also going to set up a CUPS print to PDF file queue that will print documents into OpenKM using the same method.
 +
 +
Note: This script only loads PDF files. If you want other files loaded then change the OnlyExt class to accept other file types
 +
 +
More information at [http://forum.openkm.com/viewtopic.php?f=5&t=4555&start=60 OpenKM Forum].
 +
<source lang="java">
 +
import javax.jcr.*;
 +
import com.openkm.core.*;
 +
import com.openkm.api.OKMDocument;
 +
import java.io.*;
 +
 +
String token = JcrSessionManager.getInstance().getSystemToken();
 +
Session session = JcrSessionManager.getInstance().get(token);
 +
OKMDocument document = OKMDocument.getInstance();
 +
 +
public class OnlyExt implements FilenameFilter {
 +
  String ext;
 +
 +
  public OnlyExt(String ext) {
 +
    this.ext = "." + ext;
 +
  }
 +
 +
  public boolean accept(File dir, String name) {
 +
    return name.endsWith(ext);
 +
  }
 +
}
 +
 +
 +
File scans = new File("/home/scanner");
 +
Thread.sleep(10000);  // Sleep 10 seconds in case files are still being written
 +
 +
try {
 +
  for (File scan : scans.listFiles(new OnlyExt("pdf"))) {
 +
 
 +
    try {
 +
      document.createSimple(token, "/okm:root/Scans/" + scan.getName(), new FileInputStream(scan));
 +
      scan.delete();
 +
    } catch (Exception e) {
 +
      print("Exception: " + e);
 +
    }
 +
  }
 +
 +
} catch (Exception e) {
 +
  print("Exception: " + e);
 +
}
 +
</source>
  
 
[[Category: Administration Guide]]
 
[[Category: Administration Guide]]

Latest revision as of 12:02, 22 April 2013

With this feature you can create scheduled tasks in a easy way. In this page you can see the already registered tasks. The last start and last stop columns show the last time a task was executed. If you want to see 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, an 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 are reset if you upload a new task definition. Script may be BeanShell (for a small amount of code) or a packed 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 executed, simple mark it as inactive or remove it.

Expression syntax

Commands are executed by cron when the minute, hour, and month 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 will 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();
  }
}

Nota clasica.png The crontab daemon will look for a static method called cronTask with no arguments to be executed. The string returned by this method will be included in the crontab execution result email.


Nota advertencia.png Since OpenKM 6.4 this cronTask method has an String argument with the system token.

Now create the JAR. From Eclipse you can use the contextual menu Export... and select Java > JAR file. Follow the wizard selecting the previously created class (com.openkm.sample.JarSample) as the 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 will execute the contents of the JarSample.jar file every 5 minutes. The output will be sent to the defined email address.

Users examples

Network importing

The scanner saves scans as pdfs to a shared folder ('/home/scanner') on the server then this script picks them up, loads them into an OpenKM folder ('/okm:root/Scans') and deletes the originals. I am also going to set up a CUPS print to PDF file queue that will print documents into OpenKM using the same method.

Note: This script only loads PDF files. If you want other files loaded then change the OnlyExt class to accept other file types

More information at OpenKM Forum.

import javax.jcr.*;
import com.openkm.core.*;
import com.openkm.api.OKMDocument;
import java.io.*;

String token = JcrSessionManager.getInstance().getSystemToken();
Session session = JcrSessionManager.getInstance().get(token);
OKMDocument document = OKMDocument.getInstance();

public class OnlyExt implements FilenameFilter {
  String ext;

  public OnlyExt(String ext) {
    this.ext = "." + ext;
  }

  public boolean accept(File dir, String name) {
    return name.endsWith(ext);
  }
}


File scans = new File("/home/scanner");
Thread.sleep(10000);  // Sleep 10 seconds in case files are still being written

try {
  for (File scan : scans.listFiles(new OnlyExt("pdf"))) {
   
    try {
      document.createSimple(token, "/okm:root/Scans/" + scan.getName(), new FileInputStream(scan));
      scan.delete();
    } catch (Exception e) {
      print("Exception: " + e);
    }
  }
 
} catch (Exception e) {
  print("Exception: " + e);
}