2014-10-19 12:15
浏览 102

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.


        // 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.';


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

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

        // close curl resource to free up system resources



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.


    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.

    打赏 评论

相关推荐 更多相似问题