![]() |
|
||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
MIVA® RESOURCES: Miva in Crontab
Scheduled Miva ScriptsThere 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). UNIX cronjobsWith 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:
MvSMTP and cronThe 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 Step by step instructions
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
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 ...
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 ...
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. Crontab examples
HOSTNAME=myname.com
Some useful links
Miva Empresa Unix Configuration File Reference |
||||||||||
|
Miva and some other terms used on this page are registerd trademarks of the Miva Corporation |