Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

James Robertson http://www.r0bertson.co.uk

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Create a crontab entry using XML

This code creates a formatted crontab entry using the files crontab.xml, and crontab_txt.xsl. It is intended that all cron jobs could be stored in the crontab.xml file and then passed to the crontab.

file: crontab_txt.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>

  <xsl:template match="crontab_entry">

  <xsl:apply-templates select="minutes"/>
  <xsl:apply-templates select="hours"/>
  <xsl:apply-templates select="day_of_month"/>
  <xsl:apply-templates select="months"/>
  <xsl:apply-templates select="day_of_week"/>

  <xsl:value-of select="command" />
  </xsl:template>
  
  <xsl:template match="minutes">
    <xsl:apply-templates select="minute"/>
    <xsl:if test="not(minute) or minute=''">*</xsl:if>
    <xsl:call-template name="format_unit" />
  </xsl:template>
  
  <xsl:template match="hours">
    <xsl:apply-templates select="hour"/>
    <xsl:if test="not(hour) or hour=''">*</xsl:if>
    <xsl:call-template name="format_unit" />
  </xsl:template>
  
  <xsl:template match="day_of_month">
    <xsl:apply-templates select="day"/>
    <xsl:if test="not(day) or day=''">*</xsl:if>
    <xsl:call-template name="format_unit" />
  </xsl:template>
  
  <xsl:template match="day_of_week">
    <xsl:apply-templates select="day"/>
    <xsl:if test="not(day) or day=''">*</xsl:if>
    <xsl:call-template name="format_unit" />
  </xsl:template>
  
  <xsl:template match="months">
    <xsl:apply-templates select="month"/>
    <xsl:if test="not(month) or month=''">*</xsl:if>
    <xsl:call-template name="format_unit" />
  </xsl:template>
    
  <xsl:template match="month">
    <xsl:call-template name="format_value" />
  </xsl:template>
  
  <xsl:template match="day">
    <xsl:call-template name="format_value" />
  </xsl:template>
  
  <xsl:template match="hour">
    <xsl:call-template name="format_value" />
  </xsl:template>
  
  <xsl:template match="minute">
    <xsl:call-template name="format_value" />
  </xsl:template>
  
  <xsl:template name="format_value">
    <xsl:value-of select="."/>
    <xsl:if test="not(position() =last())"><xsl:text>,</xsl:text>
    </xsl:if>
  </xsl:template>
  
  <xsl:template name="format_unit">
    <xsl:call-template name="recurring" />
    <xsl:text> </xsl:text>
  </xsl:template>
  
  <xsl:template name="recurring">
    <xsl:if test="@every">/<xsl:value-of select="@every"/>
    </xsl:if>
  </xsl:template>
  
</xsl:stylesheet>

file: crontab.xml
<crontab_entry>
  <minutes/>
  <hours every="3">
  <day_of_month>
    <day>3</day>
    <day>10</day>
    <day>15</day>
  </day_of_month>
  <months/>
  <day_of_week/>
  <command>ssh james@192.168.1.220 runjob.sh</command>
</crontab_entry>

output: * */3 3,10,15 * * ssh james@192.168.1.220 runjob.sh

A helpful crontab.xml template
<cron>
  <minutes>
    <!-- minute (0 - 59) -->
    <minute></minute>
  </minutes
  <hours>
    <!-- hour (0 -23) -->
    <hour></hour>
  </hours>
  <day_of_month>
    <!-- day of month (1 - 31) -->
    <day></day>
  </day_of_month>
  <months>
    <!-- month (1 - 12) -->
    <month></month>
  </months>
  <day_of_week>
   <!-- day of week (0 - 6) (Sunday=0 or 7) -->
   <day></day>
  </day_of_week>
  <command></command>
</cron>
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS