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 ! S" c:\xxx.exe" START-APP

Using Plugins

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 <filename>.

Here are some examples:

1) Let's create a special plugin that will ensure that at least on instance of Windows calculator is working on the computer. Place the following code in a plain text file test.spf:

\ --- 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

2) This plugin will help us start a specified application at the very last day of each month. Place the following code in a plain text file lastdayofmonth.spf:

\ --- start of lastdayofmonth.spf ---
: LAST_DAY_OF_MONTH
\ checking for a last day of the month
Year@ Mon@ MonLength Day@ =
IF
  \ starting the app (place path to your app here)
  START-APP: c:\windows\notepad.exe
THEN
;
\ --- end of lastdayofmonth.spf ---

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

INCLUDE lastdayofmonth.spf

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

# starting the app at the very last day of each month (at 12:00)
0 12 * * * !LAST_DAY_OF_MONTH

3) This plugin will help us to periodically restart an app, that was initially launched from nnCron LITE (this plugin requires WinXP, bacause it uses command line tool taskkill, that is available in this OS). Place the following code in a plain text file killapp.spf:

\ --- start of killapp.spf ---
VARIABLE KILLAPP_PID
: KILLAPP
\ killing app by it's PID
ShowMinimized
START-APP: taskkill /PID "%KILLAPP_PID @%"
\ pause for 10 seconds
10000 PAUSE
\ restarting the app (place path to your app here)
START-APP: c:\whistler\notepad.exe
\ saving PID of started application
PROC-ID KILLAPP_PID !
;
\ --- end of killapp.spf ---

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

INCLUDE killapp.spf

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

# restarting an app every day at 10:00
0 10 * * * !KILLAPP

4) The next plugin is useful if you need to start an application using 'non-standard' time interavls. Let's start Notepad every 1 hour 50 minutes. Place the following code in a plain text file interval.spf:

\ --- start of interval.spf ---
\ this is your interval (in minutes)
110 VALUE int_interval
: INTERVAL
  FT-CUR FT>MIN int_interval MOD 0=
  IF
    \ place path and name of your app here
    START-APP: notepad.exe
  THEN
;
\ --- end of interval.spf ---

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

INCLUDE interval.spf

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

# starting application every 1h 50m
* * * * * !INTERVAL

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:

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