Jump to content
xisto Community
Sign in to follow this  
ewcreators

Mimic Cronjobs... Is there a way?Please help

Recommended Posts

So, my free cpanel account (not on this server, another one) doesn't support cronjob among its awesome features.

Now i had read a post online for alternative to cron jobs, that :

Now depending on the actual functionality of the script (if its just a php page that needs to be executed). You could create a simple database that holds the last time executed, and a script included on your website that checks to see the last time it was executed and if its longer then X amount of time, execute it again. (use some ajax or something to do it behind the scenes)

Now its not the first time ive heard this. A popular, unique online web based game i play (legacy-game.net) uses this method of cron jobs.
Can anyone post a script or help for this? Is there any online script for this sort of thing?

And if you could , could you please list a proper step-by-step way to mimic cron jobs.
I sort of understand how to do it, but i dont get the part on how to manipulate time..etc. I mean to check the time with php with the time in the mysql database.

[hr=noshade] [/hr]
Nevermind , i got it. Easier then i thought!
Anyways, I'm gonna guide all the newbs and people who might need help in the future with this :

This script is for running on an hourly basis. How it is to be run, that is upto you. You can use ajax or anything. I have put it on all of my pages(basically ive put it in one page which is required in all pages) and once that page is visited, the page is run.

<?phpecho "<Br /> CRONJOB DETAILS : <br />";//create a variable which contains the current hour of the server(from 1 - 12)$timetocheck=date(g);//select the database table which contains the cronjob details$select=mysql_query("SELECT * FROM cron");$time=mysql_fetch_array($select);//setting the variable $timetoadd to the data of $timetocheck$timetoadd=$timetocheck;//echo "Last time executed : $time[last_executed]";//checking if the current time (in hour from 1-12) is not equal to the time given in the database of which the last cron was executed//if it is not, then it will initiate the required cron jobs along with updating the value for last_executed time in the database.if ($timetoadd != $time[last_executed]) {$q=mysql_query("UPDATE cron SET last_executed='$timetocheck'");require('../crons/turns.php');echo "<br/>The Following Crons have been run :</br>";echo "-Turns Update Cron (+200/h)<br />";}else {echo "<br />No cron is currently being run.<br />";}?>

Share this post


Link to post
Share on other sites

If I understand you correctly, something like this should work.

function execute(){//place your code here that you would normally put into the php page for the cronjob.}$time=5; //how often do you want this to execute (in minutes)$lasttime=mysql_result($result,0,"time"); //set this to the last time a page was loaded from the server//--------------------------------------------------------------------------------------------------$ctime=time();$time*=60;while($ctime-$time>=$lasttime){execute();ctime-=$time;}$time=time()-($ctime-$lasttime);//--------------------------------------------------------------------------------------------------mysql_query("UPDATE `name` SET `time` = ".$time);//you're sql query to add the last time loaded back into the server.

Reply if you have any other problems :lol:

Share this post


Link to post
Share on other sites

The only problem I see with your code is that if more time goes by than the cron is set for, it will only execute once. Mine will execute as many times as it would have if it was on a cron job. Both are good scripts, it just depends on what kind of script you want executed, and if its essential to happen the correct number of times.

Edited by alex7h3pr0gr4m3r (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.