0

您能否帮我在 PHP 中自定义以下 cron 脚本。该脚本正在服务器中创建许多后台进程。一天后,服务器繁忙,MYSQL 服务器崩溃。

以下脚本有什么问题。我需要这个脚本来不断地监听特定的 UDP 端口。所以它被设置为一个 cron jon,它将运行每个 mits。

set_time_limit(60);
class pingtype
{
function rotate()
{

    $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
    socket_bind($socket, '50.50.50.50', 9091);
    $from = '';
    $port = 0;
    $bytes_received = socket_recvfrom($socket, $buf, 150, MSG_PEEK, $from, $port);
    if ($bytes_received == -1)
    {
            die('An error occured while receiving from the socket');
            echo "Received $buf from $from\n";
    }

    echo "Received $buf from remote address $from and remote port $port";

    $hex_string_buf = "HEX: ".$this->strToHex($buf)."  String: ".$buf;

    $link = mysql_connect("localhost", "user_database", "123") or die ("Errror in connection ".mysql_error());
    mysql_select_db("database") or die ("Error in select ".mysql_error());

    $sql = "INSERT INTO socket SET contents='".$hex_string_buf."' ";
    mysql_query($sql) or die ("Error in query ".mysql_error());
    mysql_close($link);
    // close socket and delete own .sock file
    socket_close($socket);      
    usleep(1000);       
}
function strToHex($string)
{
    $hex='';
    for ($i=0; $i < strlen($string); $i++)
    {
        $hex .= str_pad(dechex(ord($string[$i])), 3);
    }
    return $hex;
}

function hexToStr($hex)
{
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2)
    {
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}
}



for($i=1;$i<=6000;$i++)
{
    $object_pingtype = new pingtype();  
    $object_pingtype->rotate();
}
4

1 回答 1

0

您正在超载您的服务器。如果脚本每分钟运行一次,则执行以下代码:

for($i=1;$i<=6000;$i++)
{
    $object_pingtype = new pingtype();  
    $object_pingtype->rotate();
}

这归结为每秒6000 / 60 ~ 100 个请求。如果服务器没有足够的容量,它不会在 60 秒内完成这个脚本。然后启动一个新脚本等。

在一天结束时,您的服务器将分解所有任务。

任何一个:

  • 减少循环中的旋转次数
  • 减少脚本每小时运行的次数。
于 2012-09-14T13:06:58.243 回答