5

I installed Magento 1.9, and now since the order emails are sent with cron, I want the cron to run every 1 minute, so the customer gets their emails right away.

But I do not want all the cron tasks every minute, cause it would be a huge resource drain.

How can i set up the order emails to get out every minute, but not the other cron tasks....

I also have installed AOE scheduler ?

  • set up the cron job to run one minute from deployment and 24.00001 hours henceforth. – easymoden00b Feb 16 '15 at 18:47
12

We have shops running where the cron script is called every minute and it isn't a problem resource-wise, but this is how you would do it with Aoe_Scheduler by creating separated cron groups:

  • Create a symlink cron_email.php in your Magento root directory which points to cron.php: ln -s cron.php cron_email.php

  • Define the normal cronjob for all cron tasks excluding the one sending the mail:

    */5 * * * * /usr/bin/env SCHEDULER_BLACKLIST='core_email_queue_send_all' /bin/sh /path/to/magento/cron.sh
    
  • Define the second cronjob which is only responsible for sending the mail:

    * * * * * /usr/bin/env SCHEDULER_WHITELIST='core_email_queue_send_all' /bin/sh /path/to/magento/cron.sh cron_email.php
    

One advantage you'll get when separating the crons into groups is that long running jobs in the "normal" cronjob won't keep your mails from being sent and vice versa.

As @fschmengler pointed out in the comment you'll have to be cautious when using symlinks and you are on a non-Windows system with shell_exec enabled. Here is how we actually do it to avoid problems parallel execution when using symlinks:

  • create symlinks with different names pointing to cron.php for every cron group
  • explicitly define whether the mode of the cron is always or default

The result looks like this:

crontab:

*/5 * * * * /usr/bin/env SCHEDULER_BLACKLIST='core_email_queue_send_all,long_running_cron' /bin/sh /path/to/magento/cron.sh cron.php -m=default
* * * * * /usr/bin/env /bin/sh /path/to/magento/cron.sh cron_always.php -m=always
* * * * * /usr/bin/env SCHEDULER_WHITELIST='core_email_queue_send_all' /bin/sh /path/to/magento/cron.sh cron_email.php -m=default
* * * * * /usr/bin/env SCHEDULER_WHITELIST='long_running_cron' /bin/sh /path/to/magento/cron.sh cron_longrunning.php -m=default

Magento root dir:

ls -la
cron_always.php -> cron.php
cron_email.php -> cron.php
cron_longrunning.php -> cron.php
cron.php

This makes things a little bit more complicated but it has served us well.

  • 1
    I recommend to copy instead of symlink the file. Or read this issue carefully: github.com/AOEpeople/Aoe_Scheduler/issues/43 – Fabian Schmengler Feb 16 '15 at 20:33
  • I added information on how we avoid parallel execution. – Matthias Zeis Feb 17 '15 at 8:37
  • I completely agree with Matthew in which the cron running every minute is not a significant resource drain on a proper Magento server. – kab8609 Feb 17 '15 at 22:30
2

As you have AOE Scheduler installed you can create a custom cron script (like customcron.php) which you can trigger once a minute.

<?php

require_once 'shell/abstract.php';
require 'app/Mage.php';

Mage::app('admin')->setUseSessionInUrl(false);

class ManualCron extends Mage_Shell_Abstract {
    private $_defaultCronJobs = null;

    public function __construct() {
        parent::__construct();
        $collection = Mage::getModel('aoe_scheduler/collection_crons');
        $_defaultCronJobs= array();
        foreach ($collection->getItems() as $item) {
            $data = $item->getData();
            $this->_defaultCronJobs[$data['id']] = $data['model'];
        }
    }

    private function _runCron($cronId) {
        $cron = explode("::", $this->_defaultCronJobs[$cronId]);
        $cronFunction = "Mage::getModel('".$cron[0]."')->".$cron[1]."();";
        echo "RUNNING: ".$cronFunction.PHP_EOL;
        eval($cronFunction);
    }

    private function _runAllCrons() {
        foreach ($this->_defaultCronJobs as $key => $value) {
            $this->_runCron($key);
        }
    }

    private function _displayAvaliableCronjobs() {
        foreach ($this->_defaultCronJobs as $key => $value) {
            echo $key." -> ".$value.PHP_EOL;
        }
    }

    public function run() {
        $command = $this->getArg('command');
        if($command) {
            switch($command) {
                case "all":
                    $this->_runAllCrons();
                    break;
                case "crons":
                    $this->_displayAvaliableCronjobs();
                    break;
                default:
                    if (isset($this->_defaultCronJobs[$command])) {
                        $this->_runCron($command);
                    } else {
                        echo $this->usageHelp();
                    }
                    break;
            } //switch

        } else {
            echo $this->usageHelp();
        }
    }

    public function usageHelp()
    {
        return <<<USAGE
        Usage:  php manualcron.php --command [option]

        [option]
        help                        This help

        all         Run all cron jobs
        crons    Display all the avaliable cron jobs setup
        [specific_cron_id]    Run a specific cron, e.g. "php manualcron.php  command cronjob_name" 
USAGE;
    }
}

$shell = new ManualCron();
$shell->run();
echo PHP_EOL;

Then you can configure it like: php shell/manualcron.php –command core_email_queue_send_all

Source: ecomus.co.uk

  • Its seems that the order emails are by default scheduled every minute, while the rest are for more time....I am ok with this....but all my tasks in aoe scheduler are pending....and the status never executes – styzzz Feb 17 '15 at 3:17
1

Aoe_Scheduler is capable of splitting you crons into different groups so you can run them in parallel or distribute them on multiple servers. Running multiple scheduler jobs in parallel is quite handy when you have some jobs which take a long time to run, and are not crucial for shop to run properly. En example could be a job which generates some kind of reports. Aoe Scheduler comes with whitelist/blacklist feature enabling you to do so.

How to make it work: 1. Assign a job to a group. Either from system -> scheduler -> job configuration -> edit form, or from xml configuration:

<crontab>
        <jobs>
             <job_name>
                  <run>
                    ....
                  </run>
                   <groups>reporting,other_group_name</groups>
             </job_name>
        </jobs>
</crontab>

Aoe_Scheduler will merge xml configuration with the one from db.

Configure your crontab entry using --includeGroups and --excludeGroups You can find examples in system -> scheduler -> instructions

Note: Aoe_Scheduler has evolved, and @matthias-zeis answer is not correct for newer versions of Aoe_Scheduler. The general idea of having cron groups is still valid, however the configuration should be different now.

Instead of using SCHEDULER_BLACKLIST and SCHEDULER_WHITELIST you should use configuration explained above.

-1

I found an answer all: in Magento's cron, automatically emails are sent every minutes. Thats what the schedules are for: Magento schedules emails to go out every minute.

My problem was my host was horrible at telling me how to effectively call the cron to work, and thats why the cron was not working. If you are working with BLUEHOST, and want the Magento 1.9 cron to work, you have to enter it on the server side as:

*/5 * * * * wget -O /dev/null -q http://www.YOURDOMAIN.com/[path to magento]cron.php > /dev/null

  • 2
    that is actually not the best solution, as you then have the problem to be limited via timeout and max memory which usually is set very low for frontend requests. If you have wget, why cant you instead call it with php /path/to/cron.php ? – Flyingmana Feb 17 '15 at 23:22
  • Yeah I am not sure...I have tried calling with php but it doesn't work...same path and everything.... – styzzz Feb 18 '15 at 4:21
  • using wget to call cron.php is not a good idea, the issues you are having is probably related to the cron not running as a user with proper permissions – Shapeshifter Nov 28 '16 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.