**Question : How to schedule a cron on Joomla ?
Details : I've made a component, I want this component to execute once a day.
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.
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.
“cron job every 10 seconds” Code Answer's */10 * * * * * will run every 10 sec.
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 !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With