Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joomla (3.X) How to schedule a cron?

Tags:

cron

joomla

**Question : How to schedule a cron on Joomla ?

Details : I've made a component, I want this component to execute once a day.

like image 593
Kaizoku Gambare Avatar asked Jul 14 '15 04:07

Kaizoku Gambare


People also ask

How do I set a cron schedule?

Cron jobs are scheduled at recurring intervals, specified using a format based on unix-cron. You can define a schedule so that your job runs multiple times a day, or runs on specific days and months.

How do I run a cron job every 5 seconds?

Cron only allows for a minimum of one minute. What you could do is write a shell script with an infinite loop that runs your task, and then sleeps for 5 seconds. That way your task would be run more or less every 5 seconds, depending on how long the task itself takes.

How do I run a cron job every 10 seconds?

“cron job every 10 seconds” Code Answer's */10 * * * * * will run every 10 sec.


1 Answers

Here is how to build a cron with Joomla.

As Elin says, there is no cron builded in so we have to use the unix cron. The cron will call a php script. To make the script work you need to load Joomla framework.

You can get exemple in /cli

Here is my Cron

<?php

// Initialize Joomla framework
const _JEXEC = 1;

// Load system defines
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
    require_once dirname(__DIR__) . '/defines.php';
}

if (!defined('_JDEFINES'))
{
    define('JPATH_BASE', dirname(__DIR__));
    require_once JPATH_BASE . '/includes/defines.php';
}

// Get the framework.
require_once JPATH_LIBRARIES . '/import.legacy.php';

// Bootstrap the CMS libraries.
require_once JPATH_LIBRARIES . '/cms.php';

// Load the configuration
require_once JPATH_CONFIGURATION . '/configuration.php';

require_once JPATH_BASE . '/includes/framework.php';

/**
 * Cron job 
 *
 */
class myCron extends JApplicationCli
{
    /**
     * Entry point for the script
     *
     * @return  void
     *
     * @since   2.5
     */
        public function doExecute()
        {

// YOUR CODE HERE
            require_once JPATH_BASE.'/administrator/components/com_mycom/helpers/XMLImporter.php';

            echo "CRON TASK START ";
        echo "\n"; // Use \n is you are executing the cron from the terminal.
       
            $instance = PropertyXMLImporter::instance();
            $instance->execute_import();

        echo "CRON TASK END ";
        echo "\n";
        }
    }

JApplicationCli::getInstance('myCron')->execute();
 

Now you need to schedule your cron.

Open the terminal and type

> crontab -e

If this opened as vi text editor you can press ZZ or :q! to exit then type this to get a better text editor (on mac)

> export EDITOR=nano

Then

> crontab -e

and add a line like this :

*/1 * * * * php /Applications/MAMP/htdocs/YOURPROJECT/PATH-TO-THE-CRON/cronTask.php

You can check that this is ok by typing

> crontab -l

*/1 * * * * is Cron schedule syntax, check here to read about it : https://en.wikipedia.org/wiki/Cron

In our case I used */1 to trigger it every minitues to test purpose. Of course you want to change it when it's working. In my case I want it to run everyday at 3am. So I changed it for 0 3 * * *

IMPORTANT 1 : To launch a cron job every minute for test purpose don't forget the /1 ! ( */1 * * * * ) is not the same than (1 * * * *) .

IMPORTANT 2 : If you get this error while excecution your cron on localhost "Could not connect to MySQL." Change your configuration.php with

public $host = '127.0.0.1';

instead of public $host = 'localhost'; Yes this one is a bi**h ! Lost a few hours on it ...

IMPORTANT 3 : The code I wrote above, is good if you put you cron in /cli. I'm not sure this is the best place, so remember to adapt the path to your cron location.

Stackoverflow is a huge help for me, I have benefited greatly from the anwsers of members, today I am very happy to participate to the community with this answer and I hope this will help developers like me that have no choice of working with Joomla.

I hope this can help someone !

like image 142
Kaizoku Gambare Avatar answered Nov 07 '22 17:11

Kaizoku Gambare