drgm51600 2014-07-31 08:32
浏览 53

通过cron作业运行的PHP脚本冻结。 找不到原因,请帮帮我

Background Info:

I have what seemed like a simple task, send an xml with stock and price data to an ftp location every hour. So a php script with a cron job should do the task.

This data is being retrieved from an existing magento shop. The cron setup is working because we use cron jobs for a lot of other tasks. To run do this task I set up a simple extension structure running the below code from a model. Seeing as magento recognizes the extension and the cron job is being scheduled I don't believe that the problem is magento in this case, rather something in the actual php code.

The Problem:

The code below has been getting the better of me for the last 2 days, searching google as well as Stack Overflow has not yielded results. When I run the code manually I get a result, the file is created and the script needs about 1 minute to complete. However if I schedule the cron job the script runs for about 4 hours after which I get the message :

Job was running longer than the configured max_running_time

The Code:


class VuyaniSoftware_NeckConnection_Model_StockUpdate

    public function sendStock(){

        //get the relavent products:

        $attributeVarientId = 'Ja';
        $attributeCode = 'neck_active';

        $products = Mage::getModel('catalog/product')
                               array('attribute'=>'status','eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED),

        //create the structure for the xml feed:

        $xmlDoc = new DOMDocument();

            $root = $xmlDoc ->appendChild($xmlDoc ->createElement("NECK_STOCK"));
            foreach ($products as $product) {
                $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);
                foreach($childProducts as $childProduct){
                    $ARTICLE = $root->appendChild($xmlDoc->createElement("product"));
                    $ARTICLE->appendChild($xmlDoc->createElement("mpn", $childProduct->sku));    
                    $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($childProduct)->getQty();

                    $ARTICLE->appendChild($xmlDoc->createElement("stock", round($stock,0)));
                    $ARTICLE->appendChild($xmlDoc ->createElement("salesPrice",round($product->price,2)));
                    if(($special_price !== NULL) && ($special_price !== 0)){
                        $ARTICLE->appendChild($xmlDoc ->createElement("special_price",round($product->special_price,2)));
                        $ARTICLE->appendChild($xmlDoc ->createElement("special_price",round($product->price,2)));


        //Save the feed to memory:

        $xmlDoc->formatOutput = true;

        $nameoffile="Stock_Export_".date("Ymd_His", time()+120*60).".xml";
        $data = $xmlDoc->saveXML();
        $tempHandle = fopen('php://memory', 'r+');
        fwrite($tempHandle, $data);

        // Connect to server //Job was running longer than the configured max_running_time

        $conn = ftp_connect("ftp.example.com") or die("Could not connect");

        ftp_login($conn,"***","***") or die("invelid username or password");

        // move to path where you need to upload file
        ftp_chdir($conn, "/Team Folders/NeckData/StockData") or die("could not find dir");

        ftp_pasv($conn, true);
        // upload file to particular path
        $upload = ftp_fput($conn, $nameoffile, $tempHandle, FTP_BINARY);

        if (!$upload) { echo 'FTP upload failed!'; }


I did for security reasons block out the ftp username and password as well as change the ftp server. I am sure that that data is correct in the script.

What have I tried:

  • Save the dom document with save($nameOfFile) as well as saveXML().

  • Save the file to disk in current folder as a temp location after
    which I could connect to ftp.

  • fopen(php://memory) as well as fopen(php://temp).

  • In different scenarios using either ftp_put or ftp_fput.

  • set all locations that might be used to file permission 777 (except php://memory and temp, I don't believe this is necessary?)


Here is the config.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!-- The root node for Magento module configuration -->
                <schedule><cron_expr>52 */1 * * *</cron_expr></schedule>
  • 写回答

1条回答 默认 最新

  • dongling4288 2014-08-01 10:28


    If its not help, you need to check script from frontend:

    Add next section in config.xml:


    Create file app/code/../VuyaniSoftware/NeckConnection/controllers/TestController.php with next content:

    class VuyaniSoftware_NeckConnection_TestController extends Mage_Core_Controller_Front_Action {
        public function runAction() {
            echo 'Done';

    Clear Magento Cache and open in browser:


    If all fine, you can test it via cron:

    52 * * * * /usr/bin/wget --no-check-certificate -O /var/log/cron_result.log http://your_site.com/sendstock/test/run

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



    • ¥15 相同型号电脑与配置,发现主板有一台貌似缺少了好多元器件似的,会影响稳定性和使用寿命吗?
    • ¥15 要求编写稀疏矩阵A的转置矩阵的算法
    • ¥15 编写满足以下要求的停车场管理程序,设停车场只有一个可停放n辆车的狭窄通道且只有一个大门可供车辆进出。
    • ¥15 C语言:数据子序列基础版
    • ¥20 powerbulider 导入excel文件,显示不完整
    • ¥15 用keil调试程序保证结果进行led相关闪烁
    • ¥15 paddle训练自己的数据loss降不下去
    • ¥20 用matlab的pdetool解决以下三个问题
    • ¥15 单个福来轮的平衡与侧向滑动是如何做到的?
    • ¥15 嵌入式Linux固件,能直接告诉我crc32校验的区域在哪不,内核的校验我已经找到了,uboot没有