Could you please help me to customize the following cron script in PHP. The script is creating many background process in the server. After a day the server is going busy and MYSQL server is crashed.
What is wrong with the following script. I need this script to listen to a particular UDP port continually. So it is set as a cron jon which will run every 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();
}
You are overloading your server. If the script runs every minute, then this code is executed:
This boils down to 6000 / 60 ~ 100 requests per second. If the server has not enough capacaty it will not finish this script in 60 seconds. And then a new script is started etc. etc.
In the end of the day, your server will breakdown of al the tasks.
Either: