dtypj3308 2014-10-19 12:15
浏览 112
已采纳

PHP curl脚本没有关闭连接

I have a cron job on my website that runs every so often that downloads the contents of a text file. Everything worked fine until this morning when I got an e-mail from my web host that I was using up all my (allocated) processes. They narrowed it down to this file and told me to fix it stat because it wasn't closing properly. Any help for this php newbie would be most welcome.

#!/usr/bin/php

<?php
        // create curl resource
        $ch = curl_init();

        // set url
        curl_setopt($ch, CURLOPT_URL, "http://www.address.com/textfile.txt");

        //return the transfer as a string
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        //$output contains the output string
        $output = curl_exec($ch);

        // We need to check whether the data is valid.
        // We check for the 'Client Error' string
        // inside the output data.  If it exists, we don't want
        // to overwrite the previous (good) data.

        if (strpos($output,'Client Error') !== false) {

            echo $output;
                //echo 'Client Error, no data.';

        }else{

            $outputFile = '/home/hostingacct/public_html/outputTextFile.txt';

            $fh = fopen($outputFile,'w') or die("can't open");
            fwrite($fh,$output);
            fclose($fh);
        }

        // close curl resource to free up system resources
        curl_close($ch);

?>

Edit:

Added ps xauf results - vds-status.php is my php file that I am working with.

ryan 11678  0.0  0.0 335380  6216 ?        Ss   Dec14   0:13 /usr/local/bin/php /home/ryan/etc/vds-status.php
ryan 11663  0.0  0.0 335640  6100 ?        Ss   Dec12   0:19 /usr/local/bin/php /home/ryan/etc/vds-status.php
ryan 11509  0.0  0.0 335380  6220 ?        Ss   Dec14   0:13 /usr/local/bin/php /home/ryan/etc/vds-status.php
ryan 11244  0.0  0.0 335640  6128 ?        Ss   Dec13   0:16 /usr/local/bin/php /home/ryan/etc/vds-status.php
ryan 11173  0.0  0.0 335640  6264 ?        Ss   Dec14   0:14 /usr/local/bin/php /home/ryan/etc/vds-status.php
ryan 10718  0.0  0.0 335380  6004 ?        Ss   Dec12   0:20 /usr/local/bin/php /home/ryan/etc/vds-status.php
ryan  9397  0.0  0.0 335380  6020 ?        Ss   Dec13   0:16 /usr/local/bin/php /home/ryan/etc/vds-status.php
ryan  9379  0.0  0.0 335380  6216 ?        Ss   Dec13   0:14 /usr/local/bin/php /home/ryan/etc/vds-status.php
ryan  9333  0.0  0.0 335640  6128 ?        Ss   Dec13   0:16 /usr/local/bin/php /home/ryan/etc/vds-status.php

When I posted my question previously, I had had my webhost delete all processes immediately because it was blocking my site. The problem has occurred again 2 months later, processes still running, etc. I ran ps xauf per Antoan and the results are above.

It looks like the problem started on the 12th and kept happening for two days. Can anyone shed some light on how this could happen? It seems like this happens just after midnight on those problem days. Is this possibly because the server (via cURL) is unreachable --causing the php script to not exit? Thanks everyone.

  • 写回答

1条回答 默认 最新

  • drf65218 2014-10-19 12:53
    关注

    Most probably this is wrong assumption.
    Do you have ssh access to this host?

    If yes do ps xuaf and check what processes there are there for real.

    If you post the list here we will be able to help you more.

    Update

    Looks like some kind of communication problem. Can you add those:

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); // Setting the amount of time (in seconds) before the request times out
    curl_setopt($ch, CURLOPT_TIMEOUT, 60); // Setting the maximum amount of time for cURL to execute queries 
    

    Adjust the timings if needed.

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

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站