Setting up PHP cronjobs (recurring tasks) for Joomla! extensions on Linux

With Matukio 4.3 we introduced some new time-dependent features, like automatic cancellation or confirmation of events X days before the start or automatic invoices X days after it. For these recurring tasks with PHP you need cronjobs. Cronjobs just execute a script or programm in a fixed rythm, in our case the Matukio cronjobs should be executed only once a day, but you can also set cronjobs to run every X minute or just once every week.

Please note: Windows offers something similar with Windows Task Scheduler, but in this post we are going to focus on Linux systems.

Depending on your environment there are different ways to setup cronjobs. In Joomla! installations cronjobs are normally stored in the /cli folder and they are mostly normal PHP files implementing the Joomla API.

In this first part I am going to show you how to setup cronjobs in an Linux vServer / Rootserver environment. There are, depending on the extension, different approaches for that, we are going to focus on the direct PHP execution.

Simple cronjob on a Linux server with direct PHP execution:

Login into your server with SSH - you shouldn't execute PHP scripts as root, so maybe it's a good idea to create a new normal user or depending on directory permissions you would like to use the webserver user. 


After logging into the server type:

crontab -e

If you want, you can export your favorite editor (if you haven't done so in for example your .bashrc already) with export EDITOR=nano first - if you aren't familiar with Linux command line editors, I suggest you try out Nano, which is a pretty easy and straight forward cli editor.

After hitting enter the editor opens and you see your users crontab file - in this you create the recurring tasks.


The syntax is pretty straight forward - your code begins after the comments (the lines with the # at the beginning).

Please note that every line represents one task and has six different required columns (separated by tabs).

Let's take a look at some examples:

Minute (0 - 59)  Hour (0 - 23)  Day (1 - 31)  Month (1 - 12)  Weekday (0 - 7)   Task to execute
0 0 * * *  ping -c 1 > /dev/null
0 16 * * 1  ping -c 1 > /dev/null 
30 * * * *  ping -c 1 > /dev/null 

The first one is a simple ping task that will be executed once per day at the stroke of midnight (every 24 hours / 365 days a year), like the one we want to have in Matukio (but i would suggest using a time during the day, because the Matukio cronjobs sent emails to customers, for example 0 14 * * *).

The second one will be executes once per week, every monday at 16:00 o'clock (4 pm), weekdays start with sundays (0 and 7). And finally the third one will be executed every 30 min, every day of the year. 

Now let's take a look at the task - it normally contains just one command, if you want to execute multiple at once, I would suggest writing an shell / bash script (See for more details on that topic).

In our case one command is more than enough, we just need to execute the PHP script in our cli folder. If you want you can also log the output of the script to a file (Use > for overwriting the file every execution or >> for appending to it). So here is our final line:

0 14 * * * php /path/to/your/joomal/dir/cli/matukio.php >> cronjob.log

This executes the cron script every day at 14 o'clock (2 pm) and logs the output to the cronjob.log in the home directory of the user.