cancel
Showing results for 
Search instead for 
Did you mean: 

Cron for reindexing

Cron for reindexing

Hi,

 

i'm really stuck on creating a .php file that i can run every few hours to get my site reindexing. I'm using magento 2.1 CE

 

so far i have stuck a file called cron.php in my public_html folder and am using the code below, most of it i found online apart from the following line 'magento indexer:reindex':

 

<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
require_once __DIR__ . '/app/bootstrap.php';
$status = new \Magento\Update\Status();
$cronReadinessChecker = new \Magento\Update\CronReadinessCheck();
if (!$cronReadinessChecker->runReadinessCheck()) {
exit('Cron readiness check failed');
}
if ($status->isUpdateInProgress()) {
exit('Cron is already in progress...');
}
if ($status->isUpdateError()) {
exit('There was an error in previous Updater jobs...');
}

magento indexer:reindex

$backupDirectory = BACKUP_DIR;
if (!file_exists($backupDirectory)) {
if (!mkdir($backupDirectory)) {
$status->add(sprintf('Backup directory "%s" cannot be created.', $backupDirectory));
exit();
}
chmod($backupDirectory, 0770);
}
try {
$status->setUpdateInProgress();
} catch (\RuntimeException $e) {
$status->add($e->getMessage());
exit();
}
$jobQueue = new \Magento\Update\Queue();
try {
while (!empty($jobQueue->peek()) &&
strpos($jobQueue->peek()[\Magento\Update\Queue::KEY_JOB_NAME], 'setup:') === false
) {
$job = $jobQueue->popQueuedJob();
$status->add(
sprintf('Job "%s" has been started', $job)
);
try {
$job->execute();
$status->add(sprintf('Job "%s" has successfully completed', $job));
} catch (\Exception $e) {
$status->setUpdateError();
$status->add(
sprintf('An error occurred while executing job "%s": %s', $job, $e->getMessage())
);
$status->setUpdateInProgress(false);
};
}
} catch (\Exception $e) {
$status->setUpdateError();
$status->add($e->getMessage());
} finally {
$status->setUpdateInProgress(false);
}

 

Any help on this would be greatly appreciated Smiley Happy

 

Thanks in advance!

 

Ian

4 REPLIES

Re: Cron for reindexing

or even scrap the code i put in and does anyone know the code required for the the php file to just reindex all the indexes i dont care about it doing anything else Smiley Happy

Re: Cron for reindexing

Hi @chimper

 

Have you configured a cron for your Magento installation?

Please read this documentation Configure and run cron

 

Please read following documentation Manage Indexes for more information.

 

If you have already done all these please update your question with more details to get any help.

---
Problem Solved Click Accept as Solution!:Magento Community India Forum

Re: Cron for reindexing

Thanks for the reply Smiley Happy!

 

at the minute im using cpanel and have this as my command:

 

/opt/bin/php /home/downloa1/public_html/cron.php magento indexer:reindex [indexer]

 

which im running but still no luck.

 

i basically downloaded the 2.1 version off of the site and put it all straight in to the public_html folder.Really not sure what i need to do now to make it work Smiley Sad

 

Re: Cron for reindexing

The files from Magento 2 do not belong in the public folder.

You have to place them somewhere outside of the public folder and make a symlink to your pub folder from your public_html folder. Or change the root folder for your virtual host to be the pub folder within the magento folder.

Then don't forget to deploy static files though.