Question:

I installed SuiteCRM on my server, and I want to set up a cron job to trigger PrestaShop Newsletter Pro Module's cron job script. What should I do to set up the cron job?

Answer:

Our webcron service is best for triggering SuiteCRM's cron job script. Just follow below simple steps to configure cron job for your SuiteCRM:

  1. Edit cron.php in your SuiteCRM installation, change:
    if (substr($sapi_type, 0, 3) != 'cli') {
        sugar_die("cron.php is CLI only.");
    }
    
    to
    function workcron_get_ip_address() {
        if (isset($_SERVER['HTTP_X_REAL_IP']) && !empty($_SERVER['HTTP_X_REAL_IP'])) {
            return $_SERVER['HTTP_X_REAL_IP'];
        } else if (isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP'])) {
            return $_SERVER['HTTP_CLIENT_IP'];
        } else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            return $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else if (isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR'])) {
            return $_SERVER['REMOTE_ADDR'];
        } else {
            return '';
        }
    }
    $workcron_ip = workcron_get_ip_address();
    $workcron_ip_is_from_workcron = false;
    if (!empty($workcron_ip)) {
        $workcron_bot_ips_in_json = file_get_contents('https://www.workcron.com/ips.json');
        $workcron_bot_ips = json_decode($workcron_bot_ips_in_json, true);
        if (filter_var($workcron_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
    	if (in_array($workcron_ip, $workcron_bot_ips['ipv4'])) {
                $workcron_ip_is_from_workcron = true;
    	}
        } else if (filter_var($workcron_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
            if (in_array($workcron_ip, $workcron_bot_ips['ipv6'])) {
                $workcron_ip_is_from_workcron = true;
    	}
        }
    }
    if (!$workcron_ip_is_from_workcron) {
        if (substr($sapi_type, 0, 3) != 'cli') {
            sugar_die("cron.php is CLI only.");
        }
    }
    
    Above code additionally lets webcron requests from WorkCron pass.
  2. Open your cron job dashboard, click on " Cron Job" button.
  3. In field "URL to call", enter http://www.example.com/cron.php (set cron job to run every mintue)(replace www.example.com with your install location).Checkout below screenshot:

    Set up cron job for SuiteCRM
  4. If necessary, finish the other optional settings.
  5. Click "Create Cron Job" button. You're done! WorkCron will trigger your SuiteCRM's cron job script dutifully according to your time setting.
  6. If you haven't registered your WorkCron account yet, click on the "add a password" link on the top to add an email and password to your account.
  7. If you get error:
    Uncaught exception 'Exception' with message 'cron.php running with user that is not in allowed_cron_users in config.php
    in your cron job execution logs, you need to do the following to fix it:
    1) create a php file named "get_current_user.php" with the following code:
    <?php
    echo get_current_user();
    
    2) Upload this php file to your suiteCRM server.
    3) Access this file (via URL http://yourdomain/get_current_user.php) from a web browser, copy the output of the it (e.g. xxzzyy), and add a line of below code:
    1 => 'xxzzyy',
    
    to
        'allowed_cron_users' => 
        array (
          0 => 'www-data',
        ),
    in the "config.php" file in your suiteCRM installation (remember to replace "xxzzyy" with the real output of your get_current_user.php). So that that part of code would look like:
        'allowed_cron_users' => 
        array (
          0 => 'www-data',
          1 => 'xxzzyy',
        ),
    Your suiteCRM cron job should be able to get triggered successfully from WorkCron now.
  8. If you get error:
    cron.php: running as is not allowed in allowed_cron_users in config.php. Exiting.
    in suitecrm.log, please do the following to fix it:
    1) add a line of below code:
    1 => '',
    
    to
        'allowed_cron_users' => 
        array (
          0 => 'www-data',
        ),
    in the config.php file in your suiteCRM installation. So that that part of code would look like:
        'allowed_cron_users' => 
        array (
          0 => 'www-data',
          1 => '',
        ),