A CRON job is a scheduled task that fires off a process at specified intervals without any
human intervention. CRON jobs are used on Unix and Unix-based operating systems (such
as Linux, FreeBSD). There are other scheduling systems available both for Unix-based
servers and other operating systems (such as Windows), but for the majority of web servers,
some form of CRON is by far the most commonly used scheduler.
Mambo, Joomla and nBill are all written using the PHP programming language. There is a
problem inherent in trying to run scheduled tasks on PHP applications. The reason for the
problem is a bit technical, but basically, PHP applications may require a web browser (or
something similar) to request a page before they can do anything - therefore a scheduler
cannot necessarily just execute a PHP script file directly.
Technical Note: If your server has PHP installed as a CGI instead of as an Apache
module, you can run PHP scripts from the command line by calling the PHP executable
directly with the path to the script as a parameter. However, this might not work properly,
because PHP can get confused about file paths when it is called in this way, so you still might
end up having to use one of the following workarounds.
There are workarounds for this problem. For example, a common solution is to get CRON to
fire up a web browser right there on the server (even though there is nobody around to view
the page - in fact, the server probably doesn't even have a monitor attached to it), and get the
browser to request the PHP script page (which usually will not output anything anyway).
There is a web browser called 'Lynx' which is usually used in this situation - it is a 'lightweight'
browser that ignores images, and therefore uses a lot less memory and processor resources
than a browser like Internet Explorer or Firefox would use.
The workaround to use for your server depends on your server configuration (for example, if
your server does not have the Lynx browser installed, you cannot use that as a workaround).
It is possible that your server is configured in such a way that it is just not possible to run a
PHP script via a CRON job. In this case, you would need to either ask your hosting company
to enable one of the options below (lynx, wget, fetch, or curl), perform the task yourself
manually at regular intervals, or use a less reliable method such as JCron (only reliable if you
can be sure that your website will get a steady stream of visitors every day).
Uploading the script file
First, you need to upload the script file for the job you want to run (eg. the automatic
invoice
generation script - available for download from the nBill website, or you could write
your own)
to the website root folder within your hosting account - ie. the folder where Joomla or Mambo
is installed (see below for securing the script so that it cannot be accessed by just anybody
with a web browser).
When you have uploaded the file, you need to ensure the file permissions are set correctly so
that the script can be executed. If your server has suPHP installed, setting the file
permissions to 744 is usually sufficient. Otherwise, you might need to set them to 777 (world
writeable).
Securing the script
Because the PHP script has to be called using HTTP, and needs to be able to load the nBill
environment, it cannot be hidden away in a private part of your server's directory structure - it
has to be called through Joomla or Mambo. This opens up the risk that anyone could come
along and type the URL into their browser - thus causing the script to be run independently of
CRON. The easiest way to protect against this (if using Apache as your web server - most do)
is to add some rules to your .htaccess file that prohibit anyone other than the local machine
from running the script.
Technical Note: For the following rules to work, you must have an Apache web server with
mod_rewrite enabled.
For example: If the file you want to run is called 'inv_cron_auto_invoice_generator.php',
you could add the following to your .htaccess file to ensure that nobody else can run the
script:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.0
RewriteCond %{REMOTE_ADDR} !^x\.x\.x\.x
RewriteCond %{QUERY_STRING} inv_cron_auto_invoice_generator [NC]
RewriteRule ^(.*)$ index.php [F,L]
...where x.x.x.x is the actual IP address of your server (if you have an SSL certificate, this
will be unique to your hosting account - otherwise the IP address might be shared with other
hosting accounts on the same server). You can find the IP address of your domain by running
a ping test against your domain name (in Windows, go to Start->Run, type in 'cmd' and press
enter, then type 'ping www.mydomain.com' and press enter). If you wanted to allow yourself
to run the script from a browser (useful for testing), you could add your own IP address as
well. Please note, you must add a backslash before each dot of your IP address.
Setting up the CRON job
The way you set up a CRON job on your server depends on what software you are using to
manage your hosting account. Some web hosting control panels (such as cPanel or Plesk)
offer a graphical user interface (GUI) which makes it much easier to set up the scheduled
task. If you do not have a GUI available, you will have to enter the necessary commands
using Telnet or SSH.
A quick search on the internet for 'cron' followed by the name of your web control panel
should yield plenty of articles that tell you how to physically set up a new CRON job, but it is
usually fairly self-explanatory if using something like cPanel or Plesk.
In any case, a CRON job is made up of 2 main elements: a command to be run, and an
interval definition (ie. something that tells it how often to run). You might also be asked to
provide an e-mail address to which any error messages or other output can be sent.
The command to run
The command to run is made up of 4 parts:
1) An executable (ie. a program that is capable of running PHP scripts)
2) One or more configuration options (specific to the executable)
3) An output path (where to put any output from the script)
4) A URL (the script to be called)
There are 4 different executables (programs) that could be used, and an example of what
command, options, and file path to use for each is given below. You only need one of these 4
- try each until you find one that works. If none of them work, then I'm afraid you probably
cannot run PHP scripts from a CRON job (there may be alternatives available - see link to
JCron, above).
lynx > /dev/null -dump "URL"
wget -q -O /dev/null "URL"
fetch -o /dev/null "URL"
curl -s -o /dev/null "URL"
The URL to call (shown above in red - replace with the actual URL) depends on which script
you want to execute. All CRON job script file names must start with "inv_cron_" and end with
".php". The URL to call in your CRON job command is your domain, followed by
index2.php?option=com_netinvoice&action=cron_entry&Itemid=999999999cron_file=
followed by the file name, all surrounded by quotes. For example, if using the automatic invoice generation script, your
URL might look something like this:
"http://www.example.com/index2.php?option=com_netinvoice&action=cron_entry&Itemid=999999999&cron_file=inv_cron_auto_invoice_generator.php"
Naturally, you would change example.com to your actual domain name.
For example: If you decide to use wget, the full command to enter in the CRON job would
be:
wget -q -O /dev/null
"http://www.example.com/index2.php?option=com_netinvoice&action=cron_entry&Itemid=999999999&cron_file=inv_cron_auto_invoice_generator.php"
...where example.com is changed to your actual domain name.