Pergunta

I used the following command to setup my cronjob:

sudo crontab -e -u www-data

and the actual cronjob:

*/5 * * * * /bin/sh /var/www/cron.sh >/dev/null 2>&1

I also have APC running on my site. APC CLI is on:

apc.enable_cli = 1

However, I am getting the following error. It feels like the cron may not be running with enough previlages. Should the cron be run as root? Sources advised me against this.

Cron error while executing rule_apply_all:

exception 'Zend_Cache_Exception' with message 'can't get apc memory size' in /var/www/lib/Zend/Cache.php:209
Stack trace:
#0 /var/www/lib/Zend/Cache/Backend/Apc.php(191): Zend_Cache::throwException('can't get apc m...')
#1 /var/www/lib/Zend/Cache/Backend/TwoLevels.php(518): Zend_Cache_Backend_Apc->getFillingPercentage()
#2 /var/www/lib/Zend/Cache/Backend/TwoLevels.php(192): Zend_Cache_Backend_TwoLevels->_getFastFillingPercentage('saving')
#3 /var/www/lib/Zend/Cache/Core.php(390): Zend_Cache_Backend_TwoLevels->save('1391196921', 'MAGE_AW_FUE_LOC...', Array, 1800)
#4 /var/www/lib/Varien/Cache/Core.php(76): Zend_Cache_Core->save('1391196921', 'AW_FUE_LOCK', Array, 1800, 8)
#5 /var/www/app/code/core/Mage/Core/Model/Cache.php(380): Varien_Cache_Core->save('1391196921', 'AW_FUE_LOCK', Array, 1800)
#6 /var/www/app/code/core/Mage/Core/Model/App.php(1147): Mage_Core_Model_Cache->save(1391196921, 'aw_fue_lock', Array, 1800)
#7 /var/www/app/code/local/AW/Followupemail/Model/Cron.php(98): Mage_Core_Model_App->saveCache(1391196921, 'aw_fue_lock', Array, 1800)

After a little bit of tracing, I ended up at the following:

    public function getFillingPercentage()
{
    $mem = apc_sma_info(true);
    $memSize    = $mem['num_seg'] * $mem['seg_size'];
    $memAvailable= $mem['avail_mem'];
    $memUsed = $memSize - $memAvailable;
    if ($memSize == 0) {
        Zend_Cache::throwException('can\'t get apc memory size');
    }
    if ($memUsed > $memSize) {
        return 100;
    }
    return ((int) (100. * ($memUsed / $memSize)));
}

So it must mean $memSize == 0 however when I setup a test PHP script and evaluate $memSize, it gives me the right value. So why would during a cronjob would this be different?

I have commented out in local.xml the following cache node and the cronjob is running fine. It would be ideal to understand what the following node actually does. I take it that Zend looks at this config:

    <cache>
        <backend>apc</backend>
        <prefix>MAGE_</prefix>
    </cache>
Foi útil?

Solução

If you enabled <backend>apc</backend> and your site worked fine but cron.sh was not, it could mean that you have several php installations on your server.

First of all run in console which php. This will give you the actual php that is used for running your cron php script.

For example in my case I've got /usr/bin/php.

Now run /usr/bin/php -m | grep "apc" to check if APC library is enabled. Next check /usr/bin/php -i | grep apc to get APC config.

Recommended values for Magento are

apc.enabled=1
apc.shm_size=512M
apc.num_files_hint=10000
apc.user_entries_hint=10000
apc.max_file_size=5M
apc.stat=0
apc.optimization=0
apc.shm_segments=1
apc.enable_cli=1
apc.cache_by_default=1
apc.include_once_override=1

Hope this will help you.

Outras dicas

Francis, i did some research on your problem and two solution came across: apc.enable_cli=1 and apc prefix_for_store_ in local.xml. You may need to restart Apache/Nginx after you change any APC config. Check the recipes in these articles (if you haven't already): http://www.magentocommerce.com/boards/viewthread/180339/, http://www.magentodeveloperleedsuk.co.uk/zend_cache_exception-cant-get-apc-memory-size/, http://phpbugs.wordpress.com/2012/11/03/magento-exception-zend_cache_exception-with-message-cant-get-apc-memory-size/, http://www.magecorner.com/magento-apc-cache/, http://www.qicai.us/solved-uncaught-exception-zend_cache_exception-with-message-cant-get-apc-memory-size, http://www.creation-site-lyon.com/2010/10/28/magento-cron-et-apc/(this one is in french, but you can translate it using Google).

To answer your question, no you should not need to run cron.sh as root. I cannot answer why your site works fine with APC enabled but cron.sh does not. I would recommend creating a debug script /var/www/debug.php and copy the lines from getFillingPercentage() into it:

$mem = apc_sma_info(true);
$memSize    = $mem['num_seg'] * $mem['seg_size'];
$memAvailable= $mem['avail_mem'];
$memUsed = $memSize - $memAvailable;

var_dump($mem, $memSize, $memAvailable, $memUsed);

Run this command using almost the same line as your original cron.sh, but with 3 important changes:

MAILTO="admin@example.com"  # <-- Obvi this should be a real email address. #
*/5 * * * * /bin/sh /var/www/cron.sh /var/www/debug.php 2>&1
  1. Your debug script is the first argument to cron.sh. This will be run instead of cron.php
  2. Add a MAILTO line for receiving cron output (useful for debugging)
  3. Do not pipe output to /dev/null, so the cron emails get sent (they are not sent if there is no output)

The apc.enable_cli = 1 wasn't being read from my /etc/php5/fpm/php.ini or /etc/php5/fpm/php.ini. Instead in /etc/php5/cli/conf.d/20-apcu.ini I inserted the 2 last lines:

extension=apcu.so
apc.enabled = 1
apc.enable_cli = 1

This solved the issue. This command php -i | grep 'apc' helped me realise apc.enable_cli was indeed off contrary to what I thought.

First if you have APC defined as a backend cache type or session storage in local.xml. don't. APC will be able to do what it needs without details from Magento. The cache type is known to be problematic (at least from my use). APC will cache generated OpCode from existing php/web server runtimes and cache them regardless of Magento's local.xml definition or not.

Second, try php -f /full/path/to/magento/cron.php and bypass the bash cron.sh, if you see exceptions being thrown after executing. I normally copy the crontab entry verbatim into the shell to make sure its firing off.

However, I would suspect simply removing

<cache>
    <backend>apc</backend>
    <prefix>MAGE_</prefix>
</cache>

from local.xml and/or using something like Redds or Memcache instead would be better.

Utilize apc.php to validate the APC cache will still function without the entry if you like as well.

I had the same problem. My provider told me, the cron.sh would try to change rights of apc it is not allowed to. So I removed the commands and the apc is fine as well.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top