1

We are trying to set up cron job for Magento site.

We tried this Job, but it didn't work for us:

*/5 * * * * php -f /var/www/html/sitename/cron.php

To debug we tried this Job:

*/5 * * * * /usr/bin/php  -q /var/www/html/sitename/cron.php > 
/var/www/html/sitename/cron-temp.log 2>&1

As a result, we found these errors in cron-temp.log:

PHP Warning:  require(app/bootstrap.php): failed to open stream:
 No such file or directory in /var/www/html/sitename/cron.php on line 30

    PHP Fatal error:  require(): Failed opening required 'app/bootstrap.php' 
(include_path='.:/usr/share/php:/usr/share/pear') in /var/www/html/sitename
/cron.php on line 30

cron.php

<?php


// Change current directory to the directory of current script
chdir(dirname(__FILE__));

require 'app/bootstrap.php';
require 'app/Mage.php';

if (!Mage::isInstalled()) {
    echo "Application is not installed yet, please complete install wizard first.";
    exit;
}

// Only for urls
// Don't remove this
$_SERVER['SCRIPT_NAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_NAME']);
$_SERVER['SCRIPT_FILENAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_FILENAME']);

Mage::app('admin')->setUseSessionInUrl(false);

umask(0);

$disabledFuncs = explode(',', ini_get('disable_functions'));
$isShellDisabled = is_array($disabledFuncs) ? in_array('shell_exec', $disabledFuncs) : true;
$isShellDisabled = (stripos(PHP_OS, 'win') === false) ? $isShellDisabled : true;
$isShellDisabled = true;

try {
    if (stripos(PHP_OS, 'win') === false) {
        $options = getopt('m::');
        if (isset($options['m'])) {
            if ($options['m'] == 'always') {
                $cronMode = 'always';
            } elseif ($options['m'] == 'default') {
                $cronMode = 'default';
            } else {
                Mage::throwException('Unrecognized cron mode was defined');
            }
        } else if (!$isShellDisabled) {
            $fileName = basename(__FILE__);
            $baseDir = dirname(__FILE__);
            shell_exec("/bin/sh $baseDir/cron.sh $fileName -mdefault 1 > /dev/null 2>&1 &");
            shell_exec("/bin/sh $baseDir/cron.sh $fileName -malways 1 > /dev/null 2>&1 &");
            exit;
        }
    }

    Mage::getConfig()->init()->loadEventObservers('crontab');
    Mage::app()->addEventArea('crontab');
    if ($isShellDisabled) {
        Mage::dispatchEvent('always');
        Mage::dispatchEvent('default');
    } else {
        Mage::dispatchEvent($cronMode);
    }
} catch (Exception $e) {
    Mage::printException($e);
    exit(1);
}
karel
  • 122,292
  • 133
  • 301
  • 332

3 Answers3

2
PHP Fatal error:  require(): Failed opening required 'app/bootstrap.php' 
(include_path='.:/usr/share/php:/usr/share/pear')

It looks like your required files cant be found and so it is throwing the error. I presume these files are in /var/www/html/sitename/app/? In this case add the following line at the beginning of your cron.php:

set_include_path('/var/www/html/sitename/');

You may also need to modify your require statements to include a forward slash at the beginning of the address:

require '/app/bootstrap.php';
require '/app/Mage.php';
Carrosive
  • 147
1

This is a really wild guess, but the include might be failing because of directory/file permissions.

Make sure that the user you are running the cron job for has all rights to access the directory where cron.php file is and all the files that it opens.

You might have tweaked the permissions for the webserver that runs under www-data, but you are running the cronjob from f.e. magento user.

You might want to add more information into your question about the environment (id -u) and permissions (ls -l /var/www/html/sitename/*) to further debug your issue.

oerdnj
  • 7,940
0

As others have mentioned your cron daemon is working correctly but PHP is having problems finding the required files that cron.php requires to execute. PHP looks for these in the include paths specified by php.ini and additionally in the document root for the apache module thus why the site itself works, for the command line version of PHP this additional path is the current working directory.

It should work if you make sure that PHP is executed from the document root ie change cron to run the command as follows:

*/5 * * * * cd /var/www/html/sitename && php -f /var/www/html/sitename/cron.php
MttJocy
  • 692