php 如何在 Magento 模块中设置 cron 作业?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11202463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:58:14  来源:igfitidea点击:

How to setup a cron job in Magento module?

phpxmlmagento

提问by Vinayak Garg

I wanted to setup a cron job inside my module. I followed the instructions on Magento wiki - how_to_setup_a_cron_job, but my cron job is simply not executing.

我想在我的模块中设置一个 cron 作业。我按照 Magento wiki 上的说明进行操作 - how_to_setup_a_cron_job,但我的 cron 作业根本没有执行。

This is my config.xml (app/code/local/Roomstory/Invoice/etc/config.xml)

这是我的 config.xml (app/code/local/Roomstory/Invoice/etc/config.xml)

<?xml version="1.0"?>
<config>    
    <modules>
        <Roomstory_Invoice>
            <version>0.1.1</version>
        </Roomstory_Invoice>
    </modules>
<!-- -->
    <crontab>
        <jobs>
            <roomstoryinvoice_setstatus>
                <schedule><cron_expr>*/10 * * * *</cron_expr></schedule>
                <run><model>roomstory_invoice/setstatus::run</model></run>
            </roomstoryinvoice_setstatus>
        </jobs>
    </crontab>
</config>

And this is my class. (app/code/local/Roomstory/Invoice/Model/Setstatus.php)

这是我的班级。(app/code/local/Roomstory/Invoice/Model/Setstatus.php)

<?php
class Roomstory_Invoice_Model_Setstatus {

  public function run() {
    return true;
  }

}
?>

I have installed a Cron Scheduler Module, which shows my cron job listed, but when I try to "run now" (for debugging), I get error -

我已经安装了一个 Cron 调度程序模块,它显示了我列出的 cron 作业,但是当我尝试“立即运行”(用于调试)时,出现错误 -

Invalid callback: roomstory_invoice/setstatus::run does not exist

无效回调:roomstory_invoice/setstatus::run 不存在

This something simple, after much trying, I am still not able to find the error. Please tell some other way to do it, or indicate the error in this code.

这很简单,经过多次尝试,我仍然无法找到错误。请告诉一些其他方法来做到这一点,或指出此代码中的错误。

Thanks!

谢谢!

回答by Kenny

In your modules config.xmlput the following:

在您的模块中config.xml放置以下内容:

<config>
    <global>
        <models>
            <roomstoryinvoicecron>
                <class>Roomstory_Invoice_Model</class>
            </roomstoryinvoicecron>                         
        </models>
    </global>
    <crontab>
        <jobs>
            <roomstoryinvoicecron>
                <schedule>
                    <cron_expr>*/10 * * * *</cron_expr>
                </schedule>
                <run>
                    <model>roomstoryinvoicecron/observer::setStatus</model>
                </run>
            </roomstoryinvoicecron>
        </jobs>
    </crontab>
</config>

In app/code/local/Roomstory/Invoice/Model/Observer.phpadd the following:

app/code/local/Roomstory/Invoice/Model/Observer.php添加以下内容:

<?php
class Roomstory_Invoice_Model_Observer {
    public function setStatus() {
        Mage::log("WORKS!");
    }
}

Make sure logging is enabled and it should work, check the log to be sure ;)

确保日志记录已启用并且它应该可以工作,请检查日志以确保 ;)

回答by Lance Badger

Be sure to add Magento cron.sh file in crontab

确保在 crontab 中添加 Magento cron.sh 文件

crontab -e

*/5 * * * * /bin/sh /path-to-magento/cron.sh

回答by Naresh Tank

 <crontab>
        <jobs>
            <CompanyName_ModuleName>
                <schedule>
                    <cron_expr>*/5 * * * *</cron_expr>
                </schedule>
                <run>
                    <model>ModuleName/observer::setStatus</model>
                </run>
            </CompanyName_ModuleName>
        </jobs>
    </crontab>

and create Observer.php file in Model with

并在模型中创建 Observer.php 文件

    class CompanyName_ModuleName_Model_Observer extends Mage_Core_Model_Abstract
{

   public function setStatus()
   { 

   }
}

回答by Deepak Mankotia

You can easily create a module for cron job just follow the following steps:

您可以轻松地为 cron 作业创建一个模块,只需按照以下步骤操作:

Create Config.xml file and set cron job in it.

创建 Config.xml 文件并在其中设置 cron 作业。

<?xml version="1.0"?>
<config>    
<crontab>
        <jobs>
            <Namespace_Module>
                <schedule>
                    <cron_expr>* * * * *</cron_expr>
                </schedule>
                <run>
                    <model>module/observer::method</model>
                </run>
            </Namespace_Module>
        </jobs>
    </crontab>
</config>

Your observer method:

您的观察者方法:

  class CompanyName_ModuleName_Model_Observer extends Mage_Core_Model_Abstract
{

   public function setStatus()
   { 
//your action
   }
}

now last step go to your hosting cpanel and set path and run time of cron.php file in cron job section

现在最后一步转到您的托管 cpanel 并在 cron 作业部分设置 cron.php 文件的路径和运行时间

by default you can set path like php -f /home/mercodec/public_html/cron.phpin magento.

默认情况下,您可以像php -f /home/mercodec/public_html/cron.php在 magento 中一样设置路径。

回答by Jeeva Chezhiyan

before that you have to run this script in your terminal. For ubuntu:*/1 * * * * /usr/bin/php /var/www/html/modulename/cron.php > /dev/null

在此之前,您必须在终端中运行此脚本。对于 ubuntu:*/1 * * * * /usr/bin/php /var/www/html/modulename/cron.php > /dev/null