Skip to the content
Documentation
Help Start
Reading in:
  • German: 9,090 of 9,090 pages
About the help in other languages
All help pages

By hand: a cronjob part

Add a nightly digest to mysite_hello - a CronjobPart class, the one-line script, a part of its own in cronjob.ini, running it with runcronjobs.php and scheduling it in the crontab. All PHP files passed php -l.

Download as PDF

Address: https://doc.exponential.earth/exphelp/viewer/help/extension-development/part-3-extension-developer/tutorials/by-hand-cronjob-part.md

On this page

By hand: a cronjob part

Tutorial 16. You add a digest that runs at night and writes the names of the newest articles to the extension's log. It is written as a class, the way the kernel's own cronjob scripts are since 6.0.15. The files passed php -l.

1. The class

extension/mysite_hello/classes/runnable/cronjobs/hellodigest.php:

<?php
namespace Exponential\Cronjob\Extension\MysiteHello;

/**
 * Cronjob part of mysite_hello: writes a digest of the newest articles to var/log/mysite_hello.log.
 * Registered in settings/cronjob.ini.append.php as [CronjobPart-mysite_hello] Scripts[]=hellodigest.php.
 */
class HelloDigest extends \Exponential\Runnable\CronjobPart
{
    public function run( array $scope )
    {
        $cli = $scope['cli'];
        $result = \mysiteHelloFunctionCollection::fetchNewest( 'article', 2, 10 );
        $names = array();
        foreach ( $result['result'] as $node )
        {
            $names[] = $node->attribute( 'name' );
        }
        \eZLog::write( 'Digest: ' . count( $names ) . ' newest articles: ' . implode( '; ', $names ), 'mysite_hello.log' );
        if ( empty( $scope['isQuiet'] ) )
        {
            $cli->output( 'Digest written for ' . count( $names ) . ' articles' );
        }
    }
}

It reuses the fetch collection of tutorial 11, so the digest and the page show the same articles.

2. The script runcronjobs.php runs

extension/mysite_hello/cronjobs/hellodigest.php:

<?php
return \Exponential\Cronjob\Extension\MysiteHello\HelloDigest::main( __FILE__, get_defined_vars() );

main() hands the variables runcronjobs.php defines (cli, isQuiet and the rest) to run() as $scope.

3. The part

extension/mysite_hello/settings/cronjob.ini.append.php:

<?php /* #?ini charset="utf-8"?

[CronjobSettings]
ExtensionDirectories[]=mysite_hello

[CronjobPart-mysite_hello]
Scripts[]=hellodigest.php

[AdminSettings]
CrontabSchedule_mysite_hello=30 3 * * *

*/ ?>

CrontabSchedule_mysite_hello is the schedule Setup > Cronjobs proposes for the part.

4. Run it

php -l extension/mysite_hello/classes/runnable/cronjobs/hellodigest.php
php bin/php/ezpgenerateautoloads.php -e
php bin/php/ezcache.php --clear-tag=ini
php runcronjobs.php --list | grep -A1 mysite_hello
php runcronjobs.php -s <siteaccess> mysite_hello
tail -n 1 var/log/mysite_hello.log

Expected: the part with its script in the list; Digest written for <n> articles; the digest line in the log. With -q the run prints nothing.

5. Schedule it

Setup > Cronjobs shows the part as not scheduled and proposes a crontab line with your installation's path and PHP. Add it to the crontab of the user the web server runs as:

30 3 * * * cd /path/to/installation && php runcronjobs.php -q -s <siteaccess> mysite_hello

Become an Exponential Partner

Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.

Something wrong on this page? Report an error

Documentation license

This documentation is free documentation, licensed under the GNU Free Documentation License 1.3 or later.

Exponential Copyright © 1998-2026 Exponential Foundation and 7x