Ich habe das folgende Skript, das alle 60 Sekunden. Alles funktioniert gut, aber nach zwei oder drei Stunden hört das Skript auf. Ich habe keine Idee warum.PHP While Schleife stoppt nach zwei Stunden
<?php
// The worker will execute every X seconds:
$seconds = 60;
// We work out the micro seconds ready to be used by the 'usleep' function.
$micro = $seconds * 1000000;
while(true){
try {
$url = 'URL';
$fields = array(
"hi"=> 1,
"world"=> 2,
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_exec($ch);
} catch (Exception $e) {
$myFile = "/usr/share/test/filephp.txt";
$fh = fopen($myFile, 'a') or die("Can't open file");
$stringData = "File updated at: " . time(). "\n";
fwrite($fh, $stringData);
fclose($fh);
}
// Now before we 'cycle' again, we'll sleep for a bit...
usleep($micro);
}
Das PHP-Script wird mit diesem Befehl ausgeführt:
sudo -u root php -f /usr/share/test/test.php &
Irgendwelche Ideen?
Wo Sie den obigen Code ausgeführt wird? – learner
PHP wird die Ausführung nach einer bestimmten Zeit stoppen, es sei denn, Sie führen es als Daemon –
Es ist wahrscheinlich wegen der Einstellung max_execution_time. Aber warum machst du es nicht einfach als Cron-Job statt als Endlosschleife? – Barmar