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 easycron_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 '';
        }
    }
    $easycron_ip = easycron_get_ip_address();
    $easycron_ip_is_from_easycron = false;
    if (!empty($easycron_ip)) {
        $easycron_bot_ips_in_json = file_get_contents('https://www.easycron.com/ips.json');
        $easycron_bot_ips = json_decode($easycron_bot_ips_in_json, true);
        if (filter_var($easycron_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
    	if (in_array($easycron_ip, $easycron_bot_ips['ipv4'])) {
                $easycron_ip_is_from_easycron = true;
    	}
        } else if (filter_var($easycron_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
            if (in_array($easycron_ip, $easycron_bot_ips['ipv6'])) {
                $easycron_ip_is_from_easycron = true;
    	}
        }
    }
    if (!$easycron_ip_is_from_easycron) {
        if (substr($sapi_type, 0, 3) != 'cli') {
            sugar_die("cron.php is CLI only.");
        }
    }
    
    Above code additionally lets webcron requests from EasyCron 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! EasyCron will trigger your SuiteCRM's cron job script dutifully according to your time setting.
  6. If you haven't registered your EasyCron 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 EasyCron 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 => '',
        ),