System: Cronjobs

What are cronjobs?

A “cron” can be created to perform a repetitive task at a particular time or interval; a “cronjob” is the word to generalize these tasks. Cronjobs are carried out by your web server (must be Unix-based)—therefore if in doubt, contact your hosting provider to clarify if they support cron functionalities.

Cronjobs facilitate in the everyday operation of your online shop. They can be used to automate a variety of tasks that occur at regular intervals, such as checking inventory for stock shortages or sending birthday greetings and/or newsletters to customers and partners.

Shopware offers two varieties of cronjobs. These can be used independently or together, each being programmed to perform at different times / intervals. In the current version, the difference between the two types of cronjobs can be easily recognized in the controller.

Configuration in Shopware

Shopware supports cron functionalities out-of-the-box, however the necessary plugins must be activated in advance. If you wish to make use of cron functionalities, simply install the required components by following the directions below.

Install and activate the necessary plugins

To activate cron funtionalities, open the Plugin Manager and search for Plugins called "Cron", we'll list the plugins and it's containing cronjobs here:

Cron:

  • Search: Set up a set search index (old jobs will be replaced with the refresh search index).
  • Create an email alert: Notify customers once an item becomes available in your shop.
  • Topseller Refresh: The "Topsellers" will be automatically recalculated.
  • Similar items refresh: refreshes the “You might also like” list of recommended items.
  • Refresh SEO index: Clears the SEO cache
  • Refresh search index: Clears the search cache
  • Clear the HTTP Cache: Clears the HTTP cache
  • Media Garbage Collector: Collects all unused images and put them in the trash folder, which can be cleared manually in the media manager.
  • Cancelled baskets cleanup: Deletes cancelled orders which are older than what is defined in Configuration > Basic settings > Additional settings > Privacy > Delete cancelled orders after x months.

CronBirthday:

  • Birthday greetings: Send your customers best wishes on their birthday.

CronRating:

  • Item review by email: Send the customer an item review reminder.

CronStock:

  • Inventory warning: Send a notification email once an item reaches a certain inventory level.

CronRefresh:

  • Clearing: Adjusted s_core_log, s_emarketing_lastarticles, s_search_statistics.
  • Basket Signature cleanup: Cleans up the s_order_basket_signatures and thereby resets the session of the checkout
  • Guest customer cleanup: Deletes inactive quick order "accounts"
  • Opt-In table cleanup: Cleans up the s_core_optin

The higher the number of additional cron tasks, the longer the processing time. This also means that if too many cronjobs are assigned simultaneously, some tasks might not be processed or take very long time.

The Cron plugin must be activated in order to run any Shopware cronjobs.

Cronjob management in detail

Open Configuration > Basic Settings > System > Cronjobs for an overview of cronjobs and configuration options.

  • Name (1): Name of cronjob (function).
  • Action (2): Function of cronjob.
  • Data (3): The data that results from performing the function.
  • Last & Next execution (4): Date and time of the last and next execution of the cronjob.
  • Intervall (5): Specifies the interval in seconds when the next function will be called.
  • Active (6): Turns the function on or off.
  • Disable on error (7): Disables this cronjob, when errors occur at the execution. (By default active)
  • Email recipient (8): Recipients who receive the notification by email.
  • Email template (8): Select the template used for the email notification.

Some of these cronjobs can only be carried out in combination with the CronRefresh plugin.

Cron security

By using the cron security you can prevent the execution of your cronjobs by using a key, restricting the access of an IP or only execute when a user is logged in into the backend. You can find the configuration in the Cron plugin:

  • Allowed key (1): cronjobs may only be called via a URL attribute with the valid key. The existing URL is then connected to ?key=MYKEY
  • Allowed IPs (2): cronjobs may only be called by the entered IP addresses. Several IP addresses can be added, separated by a semicolon (;)
  • Secure using account (3): cronjobs may only be called when a user is logged into the backend.

It is not possible to combine filters, as the prompt does not test for all fields. The request will pass if one of the fields contains information!

Setting up a cronjob

Cronjobs are run on your server, therefore if in doubt, please contact your hosting provider in order to determine whether or not cronjobs are possible within their realm of services.

Cronjob per browser

To execute cronjobs per browser, call the following URL in your browser:


 
// Executes all cronjobs EXCEPT newsletter
http://www.myshop.com/backend/cron
 
// Executes only the newsletter cronjob
http://www.myshop.com/backend/Newsletter/cron
 

The result can be read in your browser as text. Please note that some cronjobs might extend beyond the defined connection time or script runtime. In such cases, you can reduce the number of cronjobs, increase the execution time allowed by your hosting provider or run the cronjobs by shell / console. Alternatively you can set your cronjobs to different start times, so that every call only execute a few cronjobs to avoid timeouts.

Call per shell / console

Shell does not place any limitations on the script runtime. This also means even without a connection to a browser, cronjobs can be launched by shell or console command. You will find these settings within the management interface of your hosting provider (you can also contact them for more information on setting up cronjobs via shell).

To execute cronjobs via shell, you can use the following examples (but make sure to navigate to the Shopware directory first!):

Classic method: This method is deprecated and not recommended by us. Please use for your cronjobs our CLI-Tools.


 
// Execution directly in the Shopware directory:
php shopware.php /backend/cron
 
// (intelligent) newsletter
cd /path/toShop && php shopware.php /backend/Newsletter/cron
 

Recommended method via sw tools:


 
// Execute all pending cronjobs
php bin/console sw:cron:run
 
// Execute a cronjob by action name:
php bin/console sw:cron:run ACTION-NAME
 
// Forced execution of the Cronjobs:
php bin/console sw:cron:run ACTION-NAME -f
 

The action name can change after the first execution, but your first given action name is still valid. (e.g. Shopwarte_CronJob_Clearing > Here "Clearing" is the needed action name). Executing cronjobs by action name ignores the "active" setting in the backend, the cronjob will be executed, even if its inactive!

If you execute cronjobs via shell, please make sure that the executing user is the same which executes Shopware, otherwise it may cause permission issues.

Cronjob set up

Setting up the Cronjob call is made by the hosting, so make sure to configure it in your hosting. If you need help doing that, please contact your hoster.

We recommend that you run the cronjob every 10 – 15 minutes. To find out which PHP interpreter you are using, you can use the whichphp5 command.

Some hosts are not able to call cronjobs. Should this be the case with your hosting provider, you must create a file (i.e. cron.php) in the root directory and use one of the following examples.

Examples how to configure the cronjob

The interpreter call (#!/../../php5) is also required by your provider.


#!/usr/bin/php
<?php
echo file_get_contents('http://www.myshop.com/backend/cron');

*/15 * * * * wget -q http://www.myshop.com/backend/cron

Save the file in ASCII format & set the file permission to 755.


*     *     *     *     *  Command to be executed
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +--	Weekday (0 - 7) (Sunday is 0 and 7)
|     |     |     +------	Month (1 - 12)
|     |     +----------	Day (1 - 31)
|     +------------- 	Hour (0 - 23)
+----------------- 	Minute (0 - 59)

Special commands that are not available in the Cron plugin

Some functions of shopware can not be automatically done with the cron plugin. For this case it's usefull to use our CLI Tools - you can set up the cron as described in Call per shell / console .

HTTP Cache warmup

This command allows you to build up the http cache - its necessary after you have cleared the http cache to improve the performance of your shop. This command is not available in the shopware cron plugin, but you can easily set it up in your server administration.


 
php bin/console sw:warm:cache:http
 

More useful CLI commands

You find more usefull commands in the Shopware CLI Article.

If that does not work

Quick test

You can send the cronjob through data cleansing using one click in the backend under Configuration > Basic settings > Cronjobs. You will then receive an email with a protocol. If the email does not arrive, please check that the email path is undisturbed (i.e. not in a spam folder) or if the mail function of your hosting provider has been established.

Rights

With UNIX / Linus servers, check that both the cronjob and the files for the cronjob contain the normal privileges to run in the basic settings. The rights must be set, at minimum, to 755.

Mail functions

Normally all Linux based servers support the mail() function without requiring additional configuration. Windows servers do not support this function, therefore additional steps are required.

Set up in the backend under Configuration > Basic settings > Shop settings > Mailer and enter a Mail host. As a method, enter “smtp” as the Mail host—the Shopware server will now communicate with the SMTP host. Enter the SMTP username and password in the following fields. Check with your email provider to see whether a plain, login or encrypted CRAM-MD5 method is necessary for the transmission of the access data.

Was this article helpful?