Tags:
extract_doc1Add my vote for this tag create new tag
, view all tags

Cron for a TWikiOnWindows

Crontab file cron.tab

The "heart" of Cron is a crontab file where all the data required to start an application is stored: starting time, frequency of its execution, application's name and parameters with with it should be run. Immediately after Cron is installed, a crontab file named cron.tab will be created in nnCron's installation directory. cron.tab is an ordinary text file. It can be created and/or edited in any available text editor.

Each minute, Cron checks time of last modification of cron.tab; if it notices that file was changed, it automatically "rereads" it. If you have manually changed some data in a crontab file and you don't want to wait while Cron reloads it automatically, you can hasten the process of crontab reloading by typing in command line:

cron.exe -reload

Syntax that is used in crontab files is very simple: in order to start an application, one only has to specify a desired time of startup in cron format and (after a blank space) the command that should be executed:

time_in_cron_format command

Comments in crontab files begin with character '#' and continue until the end of line. Crontab file may contain any number of empty lines, which are ignored by the parser. Leading spaces and tab characters are also ignored.

Example:

  • application 'chime.exe' is started at 12:15 every week day
15 12 * * 1-5 d:\tools\chime.exe
  • the pdf file named in the task is opened daily
  • at 12:00 ad at 17:00
0 12,17 * * * e:\my documents\perl\re\re.pdf
  • the command file named in the task is executed every 5 minutes
*/5 * * * * d:\fido\bat\blstbbs.cmd

Please note two things:

  • when a filename contains blank spaces, there is no need to encase it in quotation
  • in order to run command and batch files and open documents, there is no need to call the command interpreter

Of course, that doesn't mean that you cannot enclose filenames with blank spaces between quotation marks or that you cannot call the command interpreter explicitly. Use the way which is more convenient to you:

0 12 * * * "e:\my documents\forth\index.html"

0 8-18 * * 1-5 cmd /c d:\tools\cleanup.bat

Cron can define local environment variables right in a crontab file. Local environment variables (i. e. variables available only to application started by Cron) are defined with word SET. See Working with Environment Variables for a more detailed description of the subject.

In command field and in SET constructs you can use the predefined variables of Cron.

Example:

30 9 * * * %COMSPEC% /c d:\tools\my_bat.bat

0 12 * * 1-5 d:\tools\maintain.bat %DD%-%MMM%-%YYYY%.log

There also exist a special type of syntax allowing to execute any Forth word as a command; this allows to execute scripts of any desired complexity, written in Forth programming language (SP-Forth). This is discussed in greater detail in chapter Cron and Forth Programing Language.


Cron Format

Cron format is a simple, yet powerful and flexible way to describe time and frequency of various actions. Cron format is extensively used in nnCron LITE.

Traditional (inherited from Unix) cron format consists of five fields separated by blank spaces:

Minute , Hour , Day_of_the_Month , Month_of_the_Year , Day_of_the_Week

nnCron LITE can use both traditional and "enhanced" version of cron format, which has an additional (6th) field: :

Minute , Hour , Day_of_the_Month , Month_of_the_Year , Day_of_the_Week , Year

A user can choose the format he would like to use by changing the value of YearField variable in cron.ini. By default, nnCron LITE uses the traditional format, which we are going to use in our examples.

The following graph shows what it consists of:

* * * * *
| | | | |
| | | | +----- Day of the Week   (range: 1-7, where 1 is Monday)
| | | +------- Month of the Year (range: 1-12)
| | +--------- Day of the Month  (range: 1-31)
| +----------- Hour              (range: 0-23)
+------------- Minute            (range: 0-59)

Any of these 5 fields may contain an asterisk (*) as their value. An asterisk would mean the entire range of possible values, i.e. each minute, each hour, etc. In the first four fields, nnCron LITE users can also use "nonstandard" character ? (question mark). A description of its functions can be found here.

Any field may contain a list of values separated by commas, (e.g. 1,3,7) or a range of values (two integers separated by a hyphen, e.g. 1-5).

After an asterisk (*) or a range of values, you can use character / to specify that values are repeated with a certain interval between them. For example, you can write "0-23/2" in Hour field to specify that some action should be performed every two hours (it will have the same effect as "0,2,4,6,8,10,12,14,16,18,20,22"); value "*/4" in Minute field means that the action should be performed every 4 minutes, "1-30/3" means the same as "1,4,7,10,13,16,19,22,25,28".

In Month and Day of Week fields, you can use names of months or days of weeks abbreviated to first three letters ("Jan,Feb,...,Dec" or "Mon,Tue,...,Sun") instead of their numeric values.

Examples:

* * * * *                         Each minute

59 23 31 12 5                     One minute  before the end of year if the last day 
                                  of the year is Friday
                           
59 23 31 Dec Fri                  Same as above (different notation)

45 17 7 6 *                       Every  year, on June 7th at 17:45

0,15,30,45 0,6,12,18 1,15,31 * *  At 00:00, 00:15, 00:30, 00:45, 06:00, 06:15, 06:30,
                                  06:45, 12:00, 12:15, 12:30, 12:45, 18:00, 18:15,
                                  18:30, 18:45, on 1st, 15th or  31st of each  month,
                                  but not on weekends

*/15 */6 1,15,31 * *              Same as above (different notation)

0 12 * * 1-5 (0 12 * * Mon-Fri)   At midday on weekdays

* * * 1,3,5,7,9,11 *              Each minute in January,  March,  May, July, September,
                                  and November

1,2,3,5,20-25,30-35,59 23 31 12 * On the  last day of year, at 23:01, 23:02, 23:03, 23:05,
                                  23:20, 23:21, 23:22, 23:23, 23:24, 23:25, 23:30,
                                  23:31, 23:32, 23:33, 23:34, 23:35, 23:59

0 9 1-7 * 1                       First Monday of each month, at 9 a.m.

0 0 1 * *                         At midnight, on the first day of each month

* 0-11 * *                        Each minute before midday

* * * 1,2,3 *                     Each minute in January, February or March

* * * Jan,Feb,Mar *               Same as above (different notation)

0 0 * * *                         Daily at midnight

0 0 * * 3                         Each Wednesday at midnight

nnCron LITE users can use an nonstandard character "?" in the first four fields of cron format. It stands for time of nnCron LITE startup, i. e. when a field is processed, startup time will be substituted for the question mark: minutes for Minute field, hour for Hour field, day of the month for Day of month field and month for Month field.

For example, if you write the following:

? ? * * *

the application will be started on nnCron LITE startup and then will be executed daily at the same time (unless the user doesn't restart nnCron LITE, of course): question marks will be "replaced" by nnCron LITE startup time. For example, if nnCron LITE was started at 8:25, question marks will be substituted in the following way:

25 8 * * *
Some more examples:

# application will be startedonly on nnCron LITE startup

? ? ? ? *

# application will be started on nnCron LITE startup (e.g. at 10:15)

# and then will be executed again each hour:

# at 11:15, 12:15, 13:15 etc.

? * * * *

# will be executed each minute after nnCron LITE startup until the next hour starts.

# The next day, if nnCron LITE has not been restarted in the meanwhile,

# it will be executed again each minute during the same hour.

* ? * * *

# will be executed daily, every five minutes,

# during the hour when nnCron LITE was started

*/5 ? * * *


Starting and Stopping nnCron LITE

If nnCron LITE is installed under Windows 9*/ME, it starts as a regular application. In Windows NT/2000/XP, nnCron LITE is started by default as a system service.

nnCron LITE home directory contains a batch file named startcron.bat, which checks the version of operation system and automatically start nnCron LITE in an appropriate way: either as a service or as a regular application.

By default, nnCron LITE service is started on system startup using built-in System account, but it can be run "in the name of" any registered user who has sufficient rights (see Note), in other words, nnCron LITE service will use this user account to log in. To do this, select nnCron LITE service in the the Services window (Control panel - Aministrative Tools - Services - cron), open Properties dialogue, select This Account on LogOn tab and enter the user name and password of the required user.

If a user wants to run nnCron LITE as a regular application under Windows NT/2000/XP, he can do this by starting nnCron LITE from command line with key -ns:

cron.exe -ns

In order to stop nnCron LITE and unload it from memory, you can use either of these two methods:

  • run batch file stopcron.bat in the program's home directory
  • run the following command from command line: cron.exe -stop (if nnCron LITE was started as a regular application rather then as a service)

Note: in order to run nnCron LITE not as System, but as а particular user, this user must have the following rights (see Control Panel - Administrative Tools - Local Security Settings - User Rights Assignment):

  • Replace a process level token
  • Act as part of the operating system
  • Bypass traverse checking
(user System has these rights by default).


Customizing nnCron LITE

nnCron LITE provides numerous options for customizing program's behavior. nnCron LITE's settings are stored in configuration file cron.ini which is located in nnCron LITE's home directory. Detailed information about variables, used in cron.ini can be found in chapter Variables Used in cron.ini. You can change nnCron LITE's settings by editing cron.ini in your favorite text editor. Attention, please: you have to restart nnCron LITE after you finish editing cron.ini to enable configuration changes.

If you keep file cron.ini when reinstalling nnCron LITE or your operation system, you will also keep intact all of your settings.

nnCron LITE and Forth Programing Language

Using Forth in Crontab Files

nnCron LITE is written in SP-Forth, which is an implementation of Forth programing language. Therefore, use of Forth has become an integral part of nnCron's make-up. As you know, crontab syntax is very simple:

<time_in_cron_format> <command>

Command in this construct is normally a name of an executable or batch file. However, those who have a good knowledge of Forth programming language (which is, as you already know, the language in which nnCron LITE is written) can use additional possibilities provided by nnCron LITE.

The command field can be occupied by a construct having the following form:

 ! any Forth words until the end of line 
"Word" in Forth is a name assigned to a definition. In other words, a Forth word is an analogue of a function (method) in other programing languages. Words are executed in the order in which they are placed in the source text.

Here is an example:

&# each Friday at 12:00 write to file 'cron.out' a list
# of Forth words from nnCron LITE dictionary
0 12 * * 5 !WORDS 
Here is another handy example:
# daily (Monday to Friday)
# close nnCron LITE at 16:45
45 16 * * 1-5 !BYE 

And finally I'll show you how to set the directory in which application should be started (working directory):

# setting c:\dir as a working directory for xxx.exe
0 * * * * !S" c:\dir" DROP APP-Dir ! START-APP: c:\dir\xxx.exe 

More complex programs written in Forth can be used nnCron LITE as plugins. Plugins are "added" (enabled) in file cron.ini by a special construct INCLUDE .

As an example, let's create a special plugin that will ensure that at least on instance of Windows calculator is working on the computer. Let's create a text file test.spf containing the following text

 \ --- start of  test.spf ---
WINAPI: FindWindowA USER32.DLL
: CALC-STARTED? ( -- ? )
Z" Calculator" 0 FindWindowA 0<>
;
: RUN-CALC
CALC-STARTED? 0= 
IF 
    ShowNormal
    START-APP: calc.exe
THEN
;
\ --- end of test.spf ---

Now we'll add the following line in file cron.ini:

INCLUDE test.spf

Now we can put the following task in file cron.tab:

# each minute check of  a Calculator is  present
# on the screen, and if no instance of a Calculator 
# is found, start a new one
* * * * * !RUN-CALC

Using Forth in Command Line (Oneliners)

Beside possessing standard capabilities for working with for command line keys, nnCron also provides its users an interesting capability to define and execute Forth words right in command line. This is similar to so-called oneliners used in Perl.

Oneliner is a line of program code that can be executed in command line.

The rules are as follows:

  • Only postfix words can be executed (although you can create new postfix word, containing several prefix words if you want to)
  • You cannot directly use loops and flow control constructs (e.g. IF ... ELSE ... THEN). However, it doesn't prevent you from defining a new word right in command line, and this definition may contain both loops and IF ... ELSE ... THEN constructs
  • You cannot execute words which output information to stdout
  • In order to compete execution of program code from command line use word BYE

Examples:

Displaying a message box with test message:

cron.exe S" test message" MsgBox BYE

Now we'll create a file, write the string 'test string' into it and display the text from the file in a message box. Of course, all this is just one long command line:

cron.exe S" oneliner.txt" FCREATE S" test string" S" oneliner.txt" FAPPEND S" oneliner.txt" FILE MsgBox BYE

Please also keep in mind that you can include in onliners entire pieces of code from external files (S" filename" INCLUDED)


nnCron LITE Predefined Variables

%hh%
- current hour (00-23)
%mm%- current minute (00-59)
%ss%

- current second (00-59)

%MM%- month (01-12)
%MMM%- month (Jan-Dec)
%DD%- day of a month (01-31)
%WW%- day of a week (Mo-Su)
%WD% - day of a week (1-7, where 1 is Monday, and 7 is Sunday)
%YYYY%- year (4 digit)
%YY%

- year (last 2 digit)


%QUOTE%- quotation mark (")
%PERCENT%- percent sign (%)

Besides, any environment variables, e.g. %PATH% or %COMSPEC% can also be used as nnCron LITE variables.

Example:

# calling command interpreter to execute 'del' command
0 * * * * %COMSPEC% /c del /S /Q c:\temp\*


Working with Environment Variables

nnCron LITE can define local environment variables right in a crontab file. Local environment variables (i. e. variables available only to application started by nnCron LITE) are defined with word SET.

# local environment variableSET varname=string value until the end of line
# or
SET varname="string value"

Environment variables defined in a crontab are totally independent of the system, and this is their main advantage. For example, in Windows NT/2000/XP, nnCron LITE is run by SYSTEM, and variables of "user" SYSTEM may be different from variables set for a current user. But if the variables are defined in crontabs, they are not affected by this problem and you won't have reenter them after reinstalling your system.

Local environment variables are reloaded, (in other words, re-set) each time any program is started by nnCron. This allows a user to set values which are difficult to set with the standard means provided by a command interpreter. For example, date, time or another changing factor can be set as an environment variable:

SET CURTIME=%hh%:%mm%
SET CURDATE=%DD%.%MM%.%YY%
SET WEEKDAY=%WD%

Please note that values of variables are "calculated" right at the moment when they are addressed, so it would be not a good idea to use constructs like the one below:

SET PATH="%PATH%;c:\xxx"

it will cause the value of PATH become longer and longer each time PATH is addressed.

Download software

Download software

  • Note: As with all executables you download from the web it is advisable to run a virus scan before using the software. -- PeterThoeny - 01 Aug 2003

-- VaidotasBarzdenas - 01 Aug 2003

Good work and thanks Vaidotas.

-- MartinCleaver -- 03 Aug 2003

Thanks Vaidotas for making this available. I moved it to the Codev web since the TWiki web mainly contains topics that get distributed with the TWiki package. Folks are more likely to find it here, and I added some cross-links.

I also restored some text Martin removed.

-- PeterThoeny - 03 Aug 2003

Sorry Peter, I agree that the warning should be there.

Better though:

  1. Apply this rule of having a warning on every executable, or
  2. Just point to (and rely on) the s/w's site carrying it.

-- MartinCleaver - 03 Aug 2003

Topic attachments
I Attachment History Action Size Date Who Comment
Microsoft Executable fileexe nncronLite.exe r1 manage 254.6 K 2003-08-01 - 13:56 VaidotasBarzdenas nncron_Lite.exe software download
Edit | Attach | Watch | Print version | History: r5 < r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r5 - 2003-08-03 - MartinCleaver
 
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2015 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.