简体   繁体   English

PHP curl脚本未关闭连接

[英]PHP curl script not closing connection

I have a cron job on my website that runs every so often that downloads the contents of a text file. 我的网站上有一项Cron作业,该作业运行频率很高,可以下载文本文件的内容。 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. 直到今天早晨,当我从Web主机收到一封电子邮件,表明我用尽了所有(已分配的)进程时,一切工作正常。 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. 此php新手的任何帮助将是最欢迎的。

#!/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. 添加了ps xauf结果-vds-status.php是我正在使用的php文件。

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. 问题在2个月后再次发生,进程仍在运行,等等。我对每个Antoan运行ps xauf,结果如上。

It looks like the problem started on the 12th and kept happening for two days. 问题似乎从12日开始,并持续发生了两天。 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? 这是否可能是因为服务器(通过cURL)无法访问-导致php脚本无法退出? Thanks everyone. 感谢大家。

Most probably this is wrong assumption. 这很可能是错误的假设。
Do you have ssh access to this host? 您对此主机有ssh访问权限吗?

If yes do ps xuaf and check what processes there are there for real. 如果是,请执行ps xuaf并检查实际存在哪些进程。

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. 如果需要,调整时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM