Views: 14020
Last Modified: 10.12.2014

It is quite often the case that some especially heavy agents have to be transferred to cron in order to be executed.

Launching Mechanism

Go to the page Settings > Tools > PHP command line and execute the following code:

COption::SetOptionString("main", "agents_use_crontab", "Y");
echo COption::GetOptionString("main", "agents_use_crontab", "N");

The code execution must result in the “Y” meaning that only periodic agents will be executed on the cron.

Go to the page Settings > System settings > Agents and make sure the column Periodical (for control). Now, edit the agents that you need by checking the box Periodical.

Add the following command to the cron:

/usr/bin/php -f /var/www/bitrix/modules/main/tools/cron_events.php

Set periodicity, for example: */10 * * * * - which means once per ten minutes.

Note: The agent launching procedure tries to cancel the limit immediately before performing the task:
@set_time_limit(0);
ignore_user_abort(true);

If set_time_limit is allowed, the execution time may exceed the value indicated in the settings of the file php.ini.

But do not forget that there are limits on the part of the host: for the amount of memory, execution time, launch periodicity, etc.

General Solution for Execution of All Agents from Cron

To begin with, we disable the execution of the agents prompted by hits. To do so, we perform the following command in the php console:

COption::SetOptionString("main", "agents_use_crontab", "N"); 
echo COption::GetOptionString("main", "agents_use_crontab", "N"); 

COption::SetOptionString("main", "check_agents", "N"); 
echo COption::GetOptionString("main", "check_agents", "Y");

The result of the execution must be "NN".

After this, we will remove the definition of the following constants from the file /bitrix/php_interface/dbconn.php:

define("BX_CRONTAB_SUPPORT", true);
define("BX_CRONTAB", true);

And add the following string in this file:

if(!(defined("CHK_EVENT") && CHK_EVENT===true))
   define("BX_CRONTAB_SUPPORT", true);

We create a file for the agent checking and sending of system messages /bitrix/php_interface/cron_events.php:

<?
$_SERVER["DOCUMENT_ROOT"] = realpath(dirname(__FILE__)."/../..");
$DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"];

define("NO_KEEP_STATISTIC", true);
define("NOT_CHECK_PERMISSIONS",true); 
define('CHK_EVENT', true);

require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");

@set_time_limit(0);
@ignore_user_abort(true);

CAgent::CheckAgents();
define("BX_CRONTAB_SUPPORT", true);
define("BX_CRONTAB", true);
CEvent::CheckEvents();

if (CModule::IncludeModule("subscribe";))
{
      $cPosting = new CPosting;
      $cPosting->AutoSend();
} 
?>

And add the following script to cron:

*/5 * * * * /usr/bin/php -f /home/bitrix/www/bitrix/php_interface/cron_events.php

After that, all agents and system message sending will be processed from cron once every 5 minutes.

Note: The execution time can be adjusted according to the project. Also, there is the possibility to “speed up” the delivery of mail notices by setting a bigger value for mail_event_bulk. This delay will be unnoticeable for users if you set up checking once per minute together with sending 100 messages at a time.

To prevent the mail message sending queue from growing it is recommended that the parameter responding for the number of mail events processed at a time be changed. To do this, perform the following command in the php console:

COption::SetOptionString("main", "mail_event_bulk", "20"); 
echo COption::GetOptionString("main", "mail_event_bulk", "5");

If the next launch of cron_events.php occurs before the completion of the previously launched scripts, no agents will be launched, and the script will complete its operation (because the agents are blocked for the time of the execution). In this case, the handling will be the same as hit handling, and a new hit may occur at the time when previous hit agents are still being executed.

Another Example

Below is a standard script code launched from cron:

#!/usr/bin/php 
<?php 
$_SERVER["DOCUMENT_ROOT"] = "/home/hosting/www"; 
$DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"]; 
define("NO_KEEP_STATISTIC", true); 
define("NOT_CHECK_PERMISSIONS", true); 
set_time_limit(0); 
define("LANG", "ru"); 
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php"); 

//your code... 

require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog_after.php"); 
?> 


Courses developed by Bitrix24