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

Cronjob parts

Work on a schedule: a cronjob script in your extension, its own part in cronjob.ini, as a plain script or a CronjobPart class, with quiet mode, logging and the stop switches.

Download as PDF

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

On this page

Cronjob parts

Part 3, chapter 20. Some work should not happen during a page request: sending a digest, importing a feed, cleaning up. An extension ships such work as a cronjob script, grouped into a part that the system's crontab runs with runcronjobs.php. The builder's view is Cronjobs for builders; this is the developer's.

Registration

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

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

[CronjobSettings]
ExtensionDirectories[]=mysite_hello

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

*/ ?>
  • ExtensionDirectories[] makes runcronjobs.php look in extension/mysite_hello/cronjobs/.
  • A part of your own lets administrators schedule your work independently (php runcronjobs.php -s site mysite_hello). You may also add your script to an existing part ([CronjobPart-infrequent]), but your own part is easier to pause, schedule and debug.

The expqueue extension does exactly this: ExtensionDirectories[]=expqueue and [CronjobPart-expqueue] with Scripts[]=expqueue.php.

Since 6.0.15 the kernel's own cronjob scripts are classes; the file in cronjobs/ is one line that hands its variables to the class. extension/mysite_hello/classes/runnable/cronjobs/hellodigest.php:

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

class HelloDigest extends \Exponential\Runnable\CronjobPart
{
    public function run( array $scope )
    {
        $cli = $scope['cli'];
        $count = \mysiteHelloFunctionCollection::fetchNewest( 'article', 2, 10 );
        $number = is_array( $count['result'] ) ? count( $count['result'] ) : 0;
        \eZLog::write( "Digest: $number new articles", 'mysite_hello.log' );
        if ( empty( $scope['isQuiet'] ) )
        {
            $cli->output( "Digest prepared for $number articles" );
        }
    }
}

extension/mysite_hello/cronjobs/hellodigest.php:

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

$scope holds the variables runcronjobs.php defines for every script, among them cli (an eZCLI) and isQuiet (the -q option). The base class is kernel/private/classes/runnable/cronjobpart.php; the pattern is described in Commands, cronjob parts and module views as classes.

The script, plain

Older scripts are plain PHP files that use the same variables directly:

<?php
// extension/mysite_hello/cronjobs/hellodigest.php
if ( !$isQuiet )
{
    $cli->output( 'Preparing the digest' );
}

They keep working. New code is easier to test as a class.

Run it

php runcronjobs.php --list | grep -A2 mysite_hello
php runcronjobs.php -s site mysite_hello
php runcronjobs.php -s site --script=hellodigest.php

Rules for cronjob code

RuleWhy
Respect -qthe crontab expects silence unless something is wrong
Write results to a log (eZLog::write()), errors with eZDebug::writeError()nobody reads cron output
Work in batches, and stop after a time limita long run overlaps with the next one
Make it safe to run twice at once, or lock itcrontabs overlap when a run is slow
Run as the web server's userfiles it writes must stay writable for the site
Never send mail to real people from a development installationuse the file mail transport there

Stop switches

Administrators can pause your part in Setup > Cronjobs, and a listener of the event cronjob/part/run can refuse to run a part (for example during a release); see Leaving out a cronjob part. You do not need to build your own switch.

RAD

The cronjob extension point's tool is Setup > Cronjobs (/setup/cronjobs), which lists scripts no part runs and activates them; the RAD catalogue entry "Cronjob script and part" describes the registration.

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