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

Writing a cronjob script

Write the script a cronjob part runs - the class-based form with CronjobPart, the variables it receives, output and quiet mode, a description for the console, batches, idempotence, users and errors.

Download as PDF

Address: https://doc.exponential.earth/exphelp/viewer/help/creators/app-creators/writing-a-cronjob-part.md

On this page

Writing a cronjob script

This page is for application creators who write the code a cronjob part runs. New scripts are classes extending Exponential\Runnable\CronjobPart, with a one-line entry file; the plain-file form of older scripts still works (A cronjob part).

Contents

  1. The class and the entry file
  2. What the script receives
  3. Output
  4. Doing the work well
  5. Testing

The class and the entry file

<?php
// extension/expdemo/classes/runnable/cronjobs/expdemoimport.php
namespace Exponential\Cronjob\Extension\Expdemo;

class ExpdemoImport extends \Exponential\Runnable\CronjobPart
{
    public function run( array $scope )
    {
        $cli = $scope['cli'];
        $result = \expDemoImportService::importNewItems( 100 );   // the work lives in a service class
        if ( empty( $scope['isQuiet'] ) )
            $cli->output( "Imported {$result['created']} items, updated {$result['updated']}" );
    }
}
<?php
/**
 * @description Import new items from the partner feed
 */
// extension/expdemo/cronjobs/expdemo_import.php
return \Exponential\Cronjob\Extension\Expdemo\ExpdemoImport::main( __FILE__, get_defined_vars() );

Regenerate the autoloads, add the script to a part (Cronjobs for applications) and run it with php runcronjobs.php -s <siteaccess> --script=expdemo_import.php.

What the script receives

get_defined_vars() passes what eZRunCronjobs::runScript() defines: $cli (an eZCLI for output) and $isQuiet. The kernel is initialized for the siteaccess given with -s; the current user is anonymous.

Output

  • One line per meaningful result; no output in quiet mode except errors.
  • $cli->error( ... ) for errors, so they stand out in logs.
  • The @description tag is what php bin/php/console list cron and Setup > Cronjobs show (Describe your own script).

Doing the work well

RuleWhy
Work in batches with a limit per runa run must end before the next one starts
Make it idempotent (remote IDs, "processed" markers)a crashed run is simply repeated
Log in a dedicated user when you write contentpermissions and the audit name a real account, not the administrator
Catch errors per item, continue, report at the endone bad record should not stop the rest
Clear in-memory caches between batcheslong runs otherwise run out of memory
Hand slow single tasks to a queuethe part stays short (Queues with expqueue)

Testing

Keep the logic in a service class and test it with PHPUnit; run the script by hand with --script on a development copy; check the output in Setup > Cronjobs (Testing applications).

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