madamada

Member
Nov 5, 2019
10
2
3
italia
cPanel Access Level
Root Administrator
hi, i can't run this script from cron i tried all possible commands but nothing. launched from terminal works from cron is not executed. I tried everything with usr / bin / bash sh usr / bin and a thousand other combinations but nothing. the script reindexes magento and works but I just can't run it from cron.

Code:
#!/bin/bash

# Smart Magento reindexing and cache clearing.  Only indexes and caches that are
# invalidate are cleared.  Place this file in your shell/ directory and make it
# executable: chmod +x magentoSmartIndexCacheRefresh.sh
#
# Before use it is recommended to consider your failures as a developer due to
# lack of ability to correctly update individual indexes and caches as records
# modified processed.
#
# Usage:
#     $ cd /var/www/html/magento
#     $ ./shell/magentoSmartIndexCacheRefresh.sh
#
# Or cron:
#     */5 * * * * cd /var/www/html/magento && ./shell/magentoSmartIndexCacheRefresh.sh >> /var/www/html/magento/var/log/smart-index-cache.log
#
# Or lazy daemon:
#     $ screen
#     $ watch -n 5 "cd /var/www/html/magento && ./shell/magentoSmartIndexCacheRefresh.sh"
#     CTRL+d
#

BIN_PHP=`which php`
BIN_GREP=`which grep`
BIN_PS=`which ps`

DIR_SHELL=`echo $0 | sed 's/magentoSmartIndexCacheRefresh\.sh//g'`

magentoPhp() {
    $BIN_PHP -r "require '$DIR_SHELL../app/Mage.php'; Mage::app(); $1"
}

reindex() {
    magentoPhp "foreach(Mage::getSingleton('index/indexer')->getProcessesCollection() as \$p) { if (\$p->getStatus() == Mage_Index_Model_Process::STATUS_RUNNING) { continue; } \$p->indexEvents(); if (\$p->getStatus() != Mage_Index_Model_Process::STATUS_REQUIRE_REINDEX) { continue; } \$p->reindexEverything(); Mage::dispatchEvent(\$p->getIndexerCode() . '_shell_reindex_after'); echo 'Reindexed ', \$p->getIndexerCode(), PHP_EOL; }"
    if [ $? -eq 0 ]; then
        echo "Required indexes reindexed"
    else
        echo "There was a problem while reindexing"
    fi
}

clearCache() {
    magentoPhp "foreach(Mage::app()->getCacheInstance()->getInvalidatedTypes() as \$k => \$c) { Mage::app()->getCacheInstance()->cleanType(\$k); echo 'Cleared ', \$k, PHP_EOL; }"
    if [ $? -eq 0 ]; then
        echo "Required caches cleared"
    else
        echo "There was a problem while clearing caches"
    fi
}

if ! $BIN_PS aux | $BIN_GREP "${DIR_SHELL}magentoSmartIndexCacheRefresh.sh" | $BIN_GREP -v grep | $BIN_GREP -v "$$" | $BIN_GREP -v watch | $BIN_GREP -v "sh -c" 1>/dev/null 2>/dev/null ; then
    reindex
    clearCache
else
    echo "Script is already running.  Not executing."
fi
 

cPDavidL

Linux Analyst II
Staff member
Oct 15, 2012
65
12
133
cPanel Access Level
Root Administrator
Greetings!

First thing I would recommend checking, is if the job is actually being executed by cron. This can be found by searching for the user in the /var/log/cron.log file.

Also, what is the actual cron job command as it is configured?(Please be sure to redact any users/etc from any paths shown)

You also may want to check the shell type allowed to that user, in WHM on the Shell Access page.