1

Is there a built-in cron job functionality in Magento and what is the suggested way to use cron job (in particular for updating stock quantities of products, but this is not important here in this question)? We have a .CSV that is periodically updated and we want the cron job to take it periodically and process it, so we want to use cron and not any other way. Cron job is way we want to go, but I want to know more about cron job integration in Magento. What is the best way to do this? I added the Magento cron using this:

crontab -e

and added this line there:

*/1 * * * * /bin/sh /Applications/XAMPP/htdocs/mymagento/cron.sh

I also created the cron using a module, like this:

<config>
    ...
    <crontab>
        <jobs>
            <mynamespace_mymodule_test>
                <schedule><cron_expr>*/1 * * * *</cron_expr></schedule>
                <run><model>mymodule/observer::test</model></run>
            </mynamespace_mymodule_test>
        </jobs>
    </crontab>
    ...
</config>

I created the observer (in Mynamespace/Mymodule/Model/Observer.php) and its method like this:

class Mynamespace_Mymodule_Model_Observer {

    public function test(){
        error_log("success!!!!!");
    }
}

So I expect to get that message logged, but it is not. It's never logged. I can see the cron in the cron_schedule table if I run [mymagento]/cron.php in the browser, but the message is not logged. What am I missing here?

  • 1
    which os are you using? – Murtuza Zabuawala Dec 5 '16 at 11:51
  • 1
    I am using mac OS Sierra 10.12.1, but I also need to know how to do this on a Linux. – Vladimir Despotovic Dec 5 '16 at 11:52
  • 1
    Ok I will answer for the linux – Murtuza Zabuawala Dec 5 '16 at 11:54
1

If you are using own linux system then fire this command in your terminal

crontab -e

After that you will see the in that file add below command

*/5 * * * * /bin/sh /[magento_path]/cron.sh and save may be using ctrl+x

For your php script you can add via below command

*/5 * * * * php -f /[magento_path]/YourPHPScriptPath/yourscriptname.php

If you are doing in host then most of the host has cronjob setup wizards from there you can setup the cronjob

OR

You can create a module and within that you can use magento cron functionality

You can find tutorial at here.

If you create module then you can see entry in cron_schedule tabel

https://stackoverflow.com/questions/11202463/how-to-setup-a-cron-job-in-magento-module

http://inchoo.net/magento/creating-cron-script-in-magento/

Be sure you added cron.sh file in your cronjob otherwise your and any magento cron will not trigger.

  • 1
    @VladimirDespotovic You have created php script for cron job ? let me update my answer – Murtuza Zabuawala Dec 5 '16 at 12:01
  • 1
    @VladimirDespotovic instead of 5 put 15 over there and it will work – Murtuza Zabuawala Dec 5 '16 at 12:04
  • 1
    @VladimirDespotovic For your custom php script you wont see any entry in cron_schedule table – Murtuza Zabuawala Dec 5 '16 at 12:45
  • 1
    @VladimirDespotovic for your custom php script you can write some value in some custom file and debug that. – Murtuza Zabuawala Dec 5 '16 at 12:46
  • 1
    @VladimirDespotovic can you provide detail code ?? – Murtuza Zabuawala Dec 6 '16 at 4:52
0

Thanks to Murtuza I figured out to replace the sh line in crontab with this one:

*/1 * * * * /Applications/XAMPP/bin/php /Applications/XAMPP/htdocs/mymagento/cron.php

I also fixed a typo with my module's model. After that, the cron works fine and the method is called periodicaly.

  • 1
    for your custom script you can try this code to. */1 * * * * /Applications/XAMPP/bin/php /[magento_path]/YourPHPScriptPath/yourscriptname.php – Murtuza Zabuawala Dec 6 '16 at 4:53

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.