//i need to run this script repetadly and the time interval can be set by a user
// checking the internet connection
if (!$sock = fsockopen("www.google.com", 80, $errno, $errstr,20))
{
?>
<script type="text/javascript">
alert("Connection problem");
</script>
<?php
}
// retriving data from database and checking the site is down
else
{
for($i=0;$i<count($res);$i++)
{
$fp=fsockopen($res[$i]['url'],80,$errno,$errstr,30);
if($fp==false)
{
?>
<script type="text/javascript">
alert("Website is down");
</script>
<?php
// writing data to database and sending mail
mail($email[0]['email_id'],"website is down",$res[$i]['url']." is down");
$inputs=array('website_id'=>$res[$i['website_id'],
'date'=>date("Y-m-d H:i:s"),'reason'=>"website down");
$obj3->addLog($inputs);
}
}
}
sleep(300);
}
Your problem with cron is this
"and the time interval can be set by a user".You can use
cronsuch way, that you run script every 30 seconds or every N seconds. That is your smallest granularity for running script.Now, if user sets that script must be run every 30 minutes, first thing you do at script is check that, if script needs to proceed – if last run was before that 30 minutes that user set. Script pseudo goes like this:
Of course this all is only necessary, if user needs to be able to dynamically change run interval and dont want to touch cron.