How do I setup a cron job a script that is part of a zend framework project

后端 未结 3 1315
难免孤独
难免孤独 2021-02-20 10:58

I have a zendframework project for which i need to run a script periodically to upload the content of a folder and another do download. The script itself is ready but I am strug

3条回答
  •  渐次进展
    2021-02-20 11:13

    Thanks all for your answers. However the solution that worked for me came from this site Howto: Zend Framework Cron. The original link is dead, but its copy can be found on Internet Archive.

    I am posting a cut of the code here. But please this is not my solution. All credits goes to the original author.

    The trick with cronjobs is that you do not want to load the whole View part of ZF, we don't need any kind of HTML output! To get this to work, I defined a new constant in the cronjob.php which I will check for in the index.php.

    cronjob.php

    define("_CRONJOB_",true);
    require('/var/www/vhosts/domain.com/public/index.php');
    // rest of your code goes here, you can use all Zend components now!
    

    index.php

    date_default_timezone_set('Europe/Amsterdam');
    
    // Define path to application directory
    defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
    
    // Define application environment
    defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
    
    // Ensure library/ is on include_path
    set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
    )));
    
    /** Zend_Application */
    require_once 'Zend/Application.php';
    
    // Create application, bootstrap, and run
    $application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
    );
    $application->bootstrap();
    
    /** Cronjobs don't need all the extra's **/
    if(!defined('_CRONJOB_') || _CRONJOB_ == false)
    {
    $application->bootstrap()->run();
    }
    

提交回复
热议问题