Cronjob

About

Cronjobs are useful for periodic tasks like updating content, sending emails (notification) and so on. But they are also useful for tasks that have a long runtime which you want to make sure that the execution timeout does not affect the script. This is typically used for import-scripts.

The main reason for implementing a import-script or similar as a cronjob is that implementing a cronjob is drastically simpler than a cli script.

What is a cronjob

Cronjobs are tasks that are run at regular intervals. See Wikipedia for a more detailed description. See also the cronjobs documentation on ez.no.

Cronjob vs CLI scripts

Cronjob and CLI scripts are similar in the way that they are both run from the command line. However, there are two important differences between CLI scripts and cronjob scripts:

  • CLI scripts can have parameters, while cronjob scripts cannot take any parameters
  • Cronjob scripts set up the environment for you. That means creating a connection to the database, activating a siteaccess and activating extensions. If you implement a CLI script, you need to do all of this yourself.

 

Examples of CLI scripts are located in ./bin/php/*.php

Examples of Cronjob scripts are located in ./cronjobs/*.php

Implementing a cronjob

In this example, we implement a simple cronjob to import content. To implement this cronjob in an extension we need the following structure:

extension/myimport
|-- cronjobs
|   `-- import.php
`-- settings
    `-- cronjob.ini.append.php

In the cronjob.ini.append.php script we add the following:

 

In the import.php:

<?php

echo "We have liftoff";

?>

We activate the extension, and now we can run the script as follows:

 

By using -dall we get the debugoutput, which is very useful for checking php-warnings and other errors.

This is the elements that needs to be in place to have a simple cronjob script. Now you can expand the import.php to do what you want it to do.

Settings

The settings for cronjobs are described in the documentation on ez.no

Running the cronjob

See the documentation on ez.no

References

List of recommended cronjobs to run

every 10min:
------------------
ezflowupdate (only if you run specific ezflow blocks that need it)

hourly:
----------
notification.php (optional - enable it if you use email notifications)
workflow.php (optional - enable it if you use delayed workflow events)

daily:
---------
ldapusermanage.php (optional - syncs ldap user accounts)
ezfoptimizeindex.php (by night - in extension ezfind)
ezflowcleanup.php

weekly:
--------------
cluster_maintenance.php (by night)
internal_draft_cleanup.php (by night)
old_draft_cleanup.php (by night)
linkcheck.php (optional - removes invalid links)
unlocked.php (by night)

Optional:
-------------
- the script ezasynchromouspublish

- cronjobs in extension ezcomments (if you use it)

Cron Jobs Alternatives

Some hosting companies don’t allow access to cron, but you can find a lot of cron jobs alternatives which are free or paid.
Here are some services: