0

我正在尝试在我的应用程序中运行 cron 作业我的设置是这样的:我的 zend 应用程序版本 1.12

在我的public/index.php里面

function Mylib_init_settings($settings, $environment)
{
    if (getenv('LOCAL_ENV') && file_exists($serverConfigFile = __DIR__ . '/../application/configs/' . getenv('LOCAL_ENV') . '.ini')) {
        $settings->addOverrideFile($serverConfigFile);
    }
}

define('MYLIB_APPLICATION_ENV', 'production');
require __DIR__ . '/../library/Mylib/Application/start.php';

内部Start.php

<?php
use Mylib\Config;
use Mylib\Config\Loader\SecondGeneration;
function mylib_trigger_hook($hook, $params = array())
{
    $func = 'mylib_init_' . strtolower(trim($hook));
    if (function_exists($func)) {
        call_user_func_array($func, $params);
    }
}
// ---------------------------------------------------------------------------------------------------------------------
// setup application constants
if (getenv('SELENIUM')) {
    defined('MYLIB_APPLICATION_ENV')
        ?: define('MYLIB_APPLICATION_ENV', 'testing');
}
// should the application be bootstrapped?
defined('MYLIB_APPLICATION_BOOTSTRAP')
    ?: define('MYLIB_APPLICATION_BOOTSTRAP', true);
// should the application run?
defined('MYLIB_APPLICATION_CREATE')
    ?: define('MYLIB_APPLICATION_CREATE', true);
// should the application run?
defined('MYLIB_APPLICATION_RUN')
    ?: define('MYLIB_APPLICATION_RUN', true);
// maximum execution time
defined('MYLIB_APPLICATION_TIME_LIMIT')
    ?: define('MYLIB_APPLICATION_TIME_LIMIT', 0);
// path to application rooth
defined('MYLIB_APPLICATION_PATH_ROOT')
    ?: define('MYLIB_APPLICATION_PATH_ROOT', realpath(__DIR__ . '/../../../'));
// path to library
defined('MYLIB_APPLICATION_PATH_LIBRARY')
    ?: define('MYLIB_APPLICATION_PATH_LIBRARY', realpath(__DIR__ . '/../../'));
mylib_trigger_hook('constants');
// ---------------------------------------------------------------------------------------------------------------------
// limits the maximum execution time
set_time_limit(MYLIB_APPLICATION_TIME_LIMIT);
// ---------------------------------------------------------------------------------------------------------------------
// determine which configuration section, and overrides to load
$configSection  = defined('MYLIB_APPLICATION_ENV') ?MYLIB_APPLICATION_ENV : null;
$configOverride = null;
$environmentFilename = MYLIB_APPLICATION_PATH_ROOT . '/environment';
if (file_exists($environmentFilename)) {
    $ini = parse_ini_file($environmentFilename);
    if ($ini === false) {
        throw new \RuntimeException('Failed to parse enviroment file: ' . $environmentFilename);
    }
    if (!defined('MYLIB_APPLICATION_ENV')) {
        // should have at least a config.section variable
        if (!isset($ini['config.section'])) {
            throw new \RuntimeException('\'config.section\' setting is missing in environment file');
        }

        $configSection = $ini['config.section'];
    }
    if (isset($ini['config.override'])) {
        $configOverrideFilename = MYLIB_APPLICATION_PATH_ROOT . '/application/configs/' . $ini['config.override'] . '.ini';
        if (!is_readable($configOverrideFilename)) {
            throw new \RuntimeException(
                sprintf('You have provided a config override file (%s), but it is not readable', $configOverrideFilename)
            );
        } else {
            $configOverride = $configOverrideFilename;
        }
    }
}
defined('MYLIB_APPLICATION_ENV')
    ?: define('MYLIB_APPLICATION_ENV', $configSection);
static $allowedEnvironmnets = array(
    'production',
    'staging',
    'testing',
    'development',
);
if (!in_array(MYLIB_APPLICATION_ENV, $allowedEnvironmnets)) {
    throw new \RuntimeException(
        sprintf('Invalid environment %s provided. Must be either of: %s', MYLIB_APPLICATION_ENV, implode(', ', $allowedEnvironmnets))
    );
}
macq_trigger_hook('environment', array(MYLIB_APPLICATION_ENV));
// ---------------------------------------------------------------------------------------------------------------------
// set the include path
set_include_path(MYLIB_APPLICATION_PATH_LIBRARY . PATH_SEPARATOR . get_include_path());
mylib_trigger_hook('includepath', array(get_include_path()));
// ---------------------------------------------------------------------------------------------------------------------
// enable PSR-0 autoloading
require_once MYLIB_APPLICATION_PATH_LIBRARY . '/Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance()
    ->setFallbackAutoloader(true);
// ---------------------------------------------------------------------------------------------------------------------
// load configuration settings, and if an override is specified, merge it
$settings = new SecondGeneration(
    MYLIB_APPLICATION_PATH_ROOT,
    MYLIB_APPLICATION_ENV,
    MYLIB_APPLICATION_PATH_LIBRARY . '/MyLib/Application/configuration.ini'
);
if ($configOverride) {
    $settings->addOverrideFile($configOverride);
}
// set up config file caching, this is a seperate cache then any application caches created!
if (isset($ini['config.cache.enabled']) && $ini['config.cache.enabled']) {
    if (isset($ini['config.cache.dir']) && is_writable($ini['config.cache.dir'])) {
        $configCache = new Zend_Cache_Core(array('automatic_serialization'=>true));
        $backend = new Zend_Cache_Backend_File(array(
            'cache_dir' => $ini['config.cache.dir'],
        ));
        $configCache->setBackend($backend);
        $settings->setCache($configCache);
        unset($configCache, $backend);
    } else {
        throw new \RuntimeException(
            sprintf('Configuration cache is enabled, but no correct cache dir is specified, or the specified directory is not writable')
        );
    }
}
// load configuration settings
Config::load($settings);
mylib_trigger_hook('settings', array($settings, MYLIB_APPLICATION_ENV));
// ---------------------------------------------------------------------------------------------------------------------
// create application and bootstrap
if (MYLIB_APPLICATION_CREATE) {

    $application = new Zend_Application(Config::environment(), Config::config());

    macq_trigger_hook('application', array($application));

    if (MYLIB_APPLICATION_BOOTSTRAP) {
        $application->bootstrap();

        macq_trigger_hook('bootstrap', array($application));
    }
    // ---------------------------------------------------------------------------------------------------------------------
    // run application?
    if (MYLIB_APPLICATION_RUN) {
        $application->run();
        macq_trigger_hook('run', array($application));
    }

}

我所做的是:

我点击了以下链接: http: //www.magenozend.com/blog/2012/02/03/setting-up-cronjobs-for-zend-framework-envoirment/

我所做的是在我的应用程序文件夹所在的级别创建一个“cron”文件夹。

在文件夹中创建了 init.php 文件,其中我添加了我的 index.php 代码和 start.php 代码。

我的控制器文件是这样的:

application/modules/myproject/controller/cronjop.php

在我刚刚调用 init.php 的 cron 作业文件中

require_once('/../../../../cron/init.php');

但是cron不工作可以有人帮助我..

提前致谢..

4

1 回答 1

1

我看到你也错过了使用 Cron 和 Zend 的意义。Zend 站点只是普通站点,因此您可以使用例如 lynx 浏览器来运行该站点。

 */10 * * * * lynx -dump http://www.myzendsite.com/mycontroller/mycronaction

只需创建My Controller 添加mycron Action 并在此方法中放入您希望 cron 执行的操作。Lynx 会像普通用户一样打开它。一段时间后,Cron 将运行 lynx。

该线路*/10表示每 10 分钟一班。您可以根据自己的需要进行调整。

还有其他方法可以运行 php 脚本,例如通过 php 解析器或 curl。

检查本教程

于 2013-06-21T13:00:04.487 回答