Miva, Miva Script, Miva Empresa, Miva Mia amd Miva Merchant are registered trademarks of the Miva Corporation
 
Ivo Truxa - truXoft control systems: advanced programming and custom IT solutions home / about / webdesign / Miva / automation / contact

http://mivo.truxoft.com
MIVO!
miva beyond limits

 

MIVA®  RESOURCES: Miva in Crontab

by Ivo Truxa, 09/5/2000 (updated 12/29/2000, 01/23/2001)

  1. Scheduled Miva Scripts
  2. UNIX cronjobs
  3. MvSMTP and cron
  4. Step by step instructions
  5. Crontab examples
  6. Links
  7. User Comments

Scheduled Miva Scripts

There is often necessity to call some Miva scripts on a regular basis or scheduled in certain time: for example regular purging of expired records from database, re-indexing or packing databases, and other maintenance tasks, sending e-mails, reading data from external sources, etc.

If you have no shell access to your server, you may implement a simple code in some of your most visited pages. This code can start other scripts (with MvDO or MvCALL) in pre-programmed manner.

Another possibility, if you do not have access to a crontab, is to call the scripts from another server or from a workstation - you can write a scheduled Windows task that dials up and call the necessary URL for you. Another good choice would be changing of your Internet Host Provider (IHP).

top


UNIX cronjobs

With a crontab you have several ways to call Miva scripts. On most Linux machines, you can use the GET command (it is part of the Perl package). Some people use other programs to call their Miva scripts: lynx, wget, webget.pl and maybe some others. My favorite way to call Miva scripts from cron is a direct system call of the Miva binary in the following way:

* * * * * /home/mydomain/www/cgi-bin/miva script.mv

Please remark the space between the Miva binary name and the script file name. There are many advantages of this method:

  • Because it is a direct system call, it bypasses all HTTP, Web server and cgi procedures and therefor it is much faster and much less resource intensive than a HTTP call.
  • It does not start any external program and therefor saves speed, memory, CPU and other resources
  • It works fine regardless if you use cgi-bin-formed URLs or the short, direct ones.
  • You do not even need to have a MIME type for the .mv extensions defined.
  • You can use any file extensions, even such that you normally do not allow on the Web
  • You can copy the miva binary using another name (e.g. miva-cron) and also copy the miva.conf (to miva-cron.conf) and use other set of parameters for your cron jobs than for Web. Like that you can disable cookies, set longer timeouts etc.

top


MvSMTP and cron

The only problem with this kind of call is that, just because it does not use the HTTP protocol (web server), there are no HTTP variables available in Miva. It means that such variables as http_host, http_referer, remote_address, server_name and some others are usually empty. In most cases it is no problem in Miva scripts designed for maintenance, but in one case it is a real problem and the reason why many developers do not use this method: Miva is unable to send e-mail.

The reason is that mail servers during the login request the caller's IP address (or domain name). Miva, when not called through HTTP, is not able to pass the proper IP (not even 127.0.0.1 or 'localhost') because it gets it from one of the HTTP environment variables. Each time you try to use the MvSMTP command with a direct system call, you will get an error message:

MvSMTP: Runtime Error: Error connecting to 'mail.yourdomain.com': HELO requires domain address: Please ensure that Miva is able to determine the server's DNS name

Fortunately there is quite simple workaround available. Miva configuration file allows to define the 'virtualhostvariable'. You can assign any available system environment variable name to this parameter. You cannot assign the value directly, it means you cannot set the parameter to the proper IP address or domain name, but you have to use some variable name that already contains the address. I was lucky - on my server there is a variable HOSTNAME that contains my server domain name. You can verify if you have this or another variable on your system if you type 'env' in your shell command prompt.

On an other system, where I installed a crontable, there was no such variable available. There is a solution - define the variable in your system or directly in the cron table. To define an environment variable in a cron table you just need to add a line in the following fromat to the beginning of the table:

VARIABLENAME=value

For example:

HOSTNAME=123.23.1.12

In the same way you could define most of the missing HTTP variables if you really need them in your cron scripts

top


Step by step instructions

  1. Check if there is any environment variable containing your server name or IP available on your system: in shell prompt (e.g. Telnet) type 'env'.
  2. If no variable available, add a new variable (e.g. HOSTNAME, SERVER_NAME or WHATEVER) and assign your server's domain name or IP address to it:
    HOSTNAME=yourdomain.com
  3. If you do not have access to your miva configuration file or you want to use another set of parameters for Miva scripts called from the cron, then you have to copy the binary to another name and create a suitable configuration file. See Miva Configuration Tips for details.
  4. Edit your miva.conf file and add the 'virtualhostvariable' parameter. For example:
    virtualhostvariable=HOSTNAME
  5. Add your cron jobs in the following way:
    10 10 * * * /home/user/you/cgi-bin/miva-cron clean.mv

The first five tokens tells when to execute the task (detailed description in 'man crontab' in Telnet or on links referred below). Path to your Miva binary follows - eiher the original one (.../cgi-bin/miva) or the copy (e.g. .../cgi-bin/miva-cron). The path can be either absolute (leading slash) or relative to your home root. The last string is the path to your Miva script (relative to the 'mivaroot' - Miva script root directory).

You can add another path if you want to redirect the output of the script to a file (or to a device) instead of being emailed to you:

8 3-10 * * 1 /usr/you/cgi-bin/miva mailing.mv > path/log.txt

For better readability, briefness and faster editing I often add more environment variables to use within the crontab:

MIVACRON=/home/user/mydomain/www/cgi-bin/mivo22
HOSTNAME=myname.com
*/5 3-10 * * * $MIVACRON remind.mv > logs/remind.log
35-55/5 * * * 3,6 $MIVACRON getrates.mv > logs/rates.log

NOTE: the virtualhostvariable value is an environment variable name, not the dns name! You have to create an environment variable of that name and assign it the right value (the domain name) in your cron table or in the system environment!

In the Miva configuration file, you should keep variables for normal use (web) and for the cron job separately. Be sure to assign the 'virtualhostvariable' for the cron job in a separate virtualhost block:

... your global Miva settings here ...
redirectonly=yes
validextensions=.mv

<VirtualHost 127.0.0.1>
virtualhostvariable=HOSTNAME
redirectonly=no
</VirtualHost>

You can also assign different values to other parameters in this block - you may want to use longer timeouts and switching off cookies, etc. If you have access to your miva.conf and arrange it in this way, you do not need to have two copies of the binary.

Cgi-bin style Miva call must be enabled with redirectonly=no for the cronjob, but because of serious security problems of this calling method, if possible it should be disabled for all other cases with redirectonly=yes in the global part of the Miva configuration.

On some servers Miva will not recognize a virtual server 127.0.0.1 correctly. You will get an error message:

Fatal Error: Miva is configured for redirected use only

In this case you have to set the parameters for Miva called from the cron, in the common global part of the Miva configuration file. To protect your server, you should disbale cgi-bin style Miva calls with setting a virtual host section with the IP (or domain name) of your webserver. If the binary can be accessed through several IP addresses, put a separate VirtualHost block for each of them:

... your cron-specifi Miva settings here ...
redirectonly=no
validextensions=.mv

<VirtualHost 222.33.123.123> # put the real IP (or domain name) of your web server here
... your Miva settings for accessing from the web here ...
virtualhostvariable=HOSTNAME
redirectonly=yes
</VirtualHost>

Alternatively, you can move the binary and configuration file dedicated for cron jobs, out of the cgi-bin directory to a directory not accessible from the web. In such case you do not need to use the redirectonly parameter at all, because there is no danger of somebody accessing it by a browser.

top


Crontab examples

HOSTNAME=myname.com
MIVACRON=/usr/local/apache/www/myname/cgi-bin/miva-cron
HOMEDIR=/usr/local/apache/www/myname

* 11 * * * $MIVACRON /batch/search.mv
01 5,9-11,17,23 * * * $MIVACRON /batch/purge.mv
01 19 * * * $MIVACRON /admin/rebill.mv
02 05 * * * $MIVACRON /admin/remind.mv
5,10 08 * * * $MIVACRON /admin/countmem.mv
10 10 * * * $MIVACRON /admin/clean.mv
08 16 * * * $MIVACRON /admin/pics_new.mv




MIVACRON=/home/truxoft/www/cgi-bin/miva-cron

*/5 * * * 2 $MIVACRON /pq/admin/stockmail.mv > logs/stock.log
*/5 0-8 * * 3 $MIVACRON /pq/admin/stockmail.mv > logs/stock.log
8,32,38 * * * * $MIVACRON /ky/rates.mv > logs/rate.log
35-55/5 0,6,7 * * * $MIVACRON /ky/mail.mv > logs/mailrate.log

top


Some useful links

Miva Empresa Unix Configuration File Reference
man 5 crontab


top

   

Miva and some other terms used on this page are registerd trademarks of the Miva Corporation
copyright  truXoft  © 1997-2008



tio e-mail.