salman_ahad@yahoo.com Posted October 27, 2009 Share Posted October 27, 2009 I need to run a php file every one hour. Is there any other solution apart from cron job? Quote Link to comment https://forums.phpfreaks.com/topic/179265-apart-from-cron/ Share on other sites More sharing options...
Daniel0 Posted October 27, 2009 Share Posted October 27, 2009 Yeah, you could mimic a task scheduling system manually using PHP to logging when it was last run and if it's over an hour ago then run it. It pretty much requires that you have constant traffic, however. Quote Link to comment https://forums.phpfreaks.com/topic/179265-apart-from-cron/#findComment-945792 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 27, 2009 Author Share Posted October 27, 2009 How can I log the time it was run? And run it again when an hour is over. A little more detail, code would help me. Quote Link to comment https://forums.phpfreaks.com/topic/179265-apart-from-cron/#findComment-945799 Share on other sites More sharing options...
Daniel0 Posted October 27, 2009 Share Posted October 27, 2009 How can I log the time it was run? $lastRun = time(); And run it again when an hour is over. if ($lastRun + 3600 < time()) { runIt(); } Quote Link to comment https://forums.phpfreaks.com/topic/179265-apart-from-cron/#findComment-945803 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 27, 2009 Author Share Posted October 27, 2009 Thanks but how do I run it for the first time? <?php //This is my code I want to run every hour. $lastRun = time(); if ($lastRun + 3600 < time()) { runIt(); $json = file_get_contents('http://some_api'); $data = json_decode($json); $ttopics = array(); foreach ($data->trends as $trend) { $tt[] = $trend->name ; } foreach ($tt as $ttkey) { echo $ttkey.'<br \>'; } } ?> I know I am making some blunder.. please correct me. Quote Link to comment https://forums.phpfreaks.com/topic/179265-apart-from-cron/#findComment-945842 Share on other sites More sharing options...
mikesta707 Posted October 27, 2009 Share Posted October 27, 2009 every time that page is run, whatever you want run will be run because you reset the $lastRun variable. Quote Link to comment https://forums.phpfreaks.com/topic/179265-apart-from-cron/#findComment-945857 Share on other sites More sharing options...
Alex Posted October 27, 2009 Share Posted October 27, 2009 For the first time just manually insert a timestamp. And make sure the stored timestamp only updates when the task is run. Quote Link to comment https://forums.phpfreaks.com/topic/179265-apart-from-cron/#findComment-945859 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 31, 2009 Author Share Posted October 31, 2009 Need more clarification on the solution someone gave me I need to run apart from cron, it is a script... Someone gave me this solution.. Make a file called "time.db" and CHMOD it as 0777 PHP Code: <?php // name of your file $myFile="time.db"; $time=file($myFile); if(time()-3600 > $time[0]){ // an hour has elapsed // do your thing. // write the new timestamp to file $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, time()); fclose($fh); } else{ // it hasn't been an hour yet, so do nothing } ?> I am not sure if Godaddy allows me chmod any file... need more clarification. Quote Link to comment https://forums.phpfreaks.com/topic/179265-apart-from-cron/#findComment-948224 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 31, 2009 Author Share Posted October 31, 2009 My script obviously did not run the next time...PLEASE CORRECT ME require_once("includes/connection.php"); $query = mysql_query("SELECT lastRun FROM time") or die(mysql_error()); if(mysql_num_rows($query)!=0) { while($row = mysql_fetch_array($query)) { $TimeRun = $row['lastRun']; if ($TimeRun + 3600 < time()) { $to_email = "salman_ahad@yahoo.com"; $subject = "test script"; $message = "Time : Running"; mail($to_email, $subject, $message); $TimeRun = time(); $querypost = mysql_query ("UPDATE time SET lastRun = '$TimeRun'"); } } } else{ //nothing } OR..could I do? function first(){ //All my code sleep(3600); call function second() } function second(){ //All my same code as above sleep(3600); call function first() } Quote Link to comment https://forums.phpfreaks.com/topic/179265-apart-from-cron/#findComment-948246 Share on other sites More sharing options...
PFMaBiSmAd Posted October 31, 2009 Share Posted October 31, 2009 Perhaps if you stated why you cannot use a cron job on your godaddy hosting - http://help.godaddy.com/article/3547 Quote Link to comment https://forums.phpfreaks.com/topic/179265-apart-from-cron/#findComment-948297 Share on other sites More sharing options...
salman_ahad@yahoo.com Posted October 31, 2009 Author Share Posted October 31, 2009 .....cron job on your godaddy hosting any cron job allows us to run a php file at specific time...but this is what I am trying to do //test.php //users table contains multiple users //message table contains multiple messages $user = mysql_query("SELECT * FROM users") or die(mysql_error()); $message = mysql_query("SELECT * FROM message") or die(mysql_error()); //...guide me for rest of the code //Every user should be sent a message every one hour. Also the message which was not sent to any user. //Lets say user-1 was sent a message-23 at 01:02:03 // user-1 shld receive the next message in queue after one hour user-2 was sent a message-40 at 20:22:00 // user-2 shld receive the next message in queue after one hour ... user-n was sent a message-78 at 12:30:10 // user-n shld receive the next message in queue after one hour Can this user specific script be run as cron?? Quote Link to comment https://forums.phpfreaks.com/topic/179265-apart-from-cron/#findComment-948451 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.