Jump to content

[SOLVED] A sort of cron jobs.


John_S

Recommended Posts

Hello everybody,I am trying to write a function which would work similarly to a cron job, each time a visitor will come to my site, the cron function will be called and will check if there is a cronjob to execute. I want to store all the cronjobs within a flat file so there won't be too many connections to the database. However when I test my function it refuses to work, it doesn't output any errors at all, but there is no result displayed neither the flat file database is changed.

Could somebody please tell me what's wrong? Here's my code:

function cron($name) {
        $file = file('./crons/crons.db.php');
        foreach ($file as $line) {
             if(ereg("$name", $line)) {                   $cronjob = explode("|", $line);                   if (time() >= $cronjob[2]) {                          include("./crons/$name.cron.php");                   }             }         } // End of Foreach
        $source = fopen('./crons/crons.db.php', w);
        foreach ($file as $line) {                 if(!ereg("$name", $line)) {                     fwrite($source, $line);                 } else {                      $lastrun = time();                     $toberun = time() + (24 * 60 * 60);                    fwrite($source, "|$name|$lastrun|$toberun|$status| "); }      }   } // End of Fucntion

In my files, I call the function like this: cron("db_backup"); [db_backup = databse backup.]

Thanks a lot in advance!Yours John_S

Link to comment
Share on other sites

<?php

function cron($name) {
$file = file('./crons/crons.db.php');

foreach ($file as $line) {
     if(ereg("$name", $line)) {
	 	$cronjob = explode("|", $line);
		if (time() >= $cronjob[2]) {
			include("./crons/$name.cron.php");
		}
	}
}//End of Foreach

$source = fopen('./crons/crons.db.php', w);

foreach ($file as $line) {          
	if(!ereg("$name", $line)) {                     
		fwrite($source, $line);                 
	} else {
		$lastrun = time();
		$toberun = time() + (24 * 60 * 60);
		fwrite($source, "|$name|$lastrun|$toberun|$status| ");
	}

}
}//End of Fucntion

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.