Agents is a feature of 1C-Bitrix that allows you to automate regularly executed processes
    An agent is a record in the site database that contains information about a run at a certain point in time

    By default, agents are configured to be launched on Event, i.e. with any user activity 1C-Bitrix analyzes the list of agents and launches those of them, whose launch time has come

    In order to make the scripts run on schedule, the agents must be switched to Cron

    To begin with, we will completely disable the execution of the agents on the hit. To do this, we will run 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 should be "NN".

    After that remove the definition of the following constants from /bitrix/php_interface/dbconn.php file:

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

    And add to this file:

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

    Create a file for checking agents and sending 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();
    } 
    ?&gt
    

    And add this script to the cron:

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

    After this all agents and sending system events will be handled from under the cron, once every 5 minutes.

    • Note: The execution time can be adjusted according to the project. In addition, it is possible by setting a large value of mail_event_bulk to make the delivery of mail notifications "faster". Setting a check every minute, along with sending 100 messages at a time, will make this delay unnoticeable to users.

    To avoid an increase in the mail queue, it is recommended to change the parameter responsible for the number of mail events processed at a time. To do this, run the following command in the php console:

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

    If the cron_events.php was started before the first script was finished, it will not start agents and will terminate. (Since agents are blocked for the duration of execution.) In this case, processing is no different from processing on a hit; a new hit may happen at a time when agents on the previous one have not yet worked.