PDA

View Full Version : help with a simple loop


MrNase
Mon 31st May '04, 10:12am
Hey,

Here's what i want: A very simple loop that checks every 5 minutes if a script is on the server. Why that? Iam waiting so badly for my new server and i don't want to check every 5 minutes on my own ;)

Here's what i already got:

function checkit() {
$dummy = @fopen("http://www.validhtml.com/phpinfo.php","r");

if(!$dummy)
{
$ergebnis = "no";
}
else
{
$ergebnis = "yes";
}

return $ergebnis;
}



for ($i = 1; $i <= 10; $i++) {

echo checkit();
sleep('1');

$i++;


}



But that doesn't work as expected...
I want the script to check every 5 minutes and echo the status everytime the scripts checks for the file.
By now, the script checks for the file, waits a minute and checks it again and so on but it doesn't echo the status... The status is echoed when the script ran successfully (after 10 seconds -> 10 checks).

I want to have
status (5 minutes break)
status (5 minutes break)
[...]

and not
(5 minutes break) statusstatusstatusstatusstatusstatusstatusstatusst atusstatus


Hope you understood what i want ;)

sirspot
Mon 31st May '04, 3:08pm
<?php
set_time_limit(0);
$scriptURL = 'http://www.validhtml.com/phpinfo.php';
$sleepMinutes = 5;
$sleepOneMinute = 60;
$exitOnSuccess = true;
do
{
$canOpenScript = @fopen($scriptURL, 'rb');
if($canOpenScript)
{
echo "Success\n";
fclose($canOpenScript);
}
else
{
echo "Failure - waiting $sleepMinutes minute(s)\n";
for($i=0;$i<($sleepMinutes*2);$i++)
{
sleep($sleepOneMinute/2);
echo ".";
flush();
}
echo "\n";
}
flush();
}while(($canOpenScript == false) && ($exitOnSuccess));
?>

MrNase
Mon 31st May '04, 3:19pm
wow, thats very nice :)

Looks good but doesn't work :(

I saved it as check.php and opened it in my browser.. the page is fully loaded but with no content... firefox seems to be frozen...

Can i extend it so that i writes the time + status to a textfile?

sirspot
Tue 1st Jun '04, 11:28am
I didn't test it in a web page...I figured you would be executing it from a command line. You can add the appropriate fopen, fwrite, and fclose commands to write to a data log or if you are on linux, just redirect the output to a file and run the script in the background.

-Adam

MrNase
Tue 1st Jun '04, 11:32am
It doesn't matter anymore. My server is online :D

Thank you anyway :)