My Webhost Package only allows me one Cronjob (I bought an inexpensive version). I've created a multsite domain using a subdomain with a single codebase and separate databases. I've written a PHP-Script called multicron.php which is called by the system cron and in turn cycles through my subdomains one-by-one with each call. I did it this way so that the system would not be overrun with several simultaneous cron requests.

/*Read Write length of 2 limits subdomains to 99 */

$cfname="multicron.dat";
if (file_exists($cfname)) {
   $cfhandle = fopen($cfname,"r+");
   $croncounter =fgets($cfhandle,2);
} else {
   $cfhandle = fopen($cfname,"w");
   $croncounter="0";
}

switch($croncounter) {
   case "0":
       $rstr = system("/usr/bin/wget -O - -q http://mysite.com/cron.php",$rval);
       break;
   case "1":
       $rstr = system("/usr/bin/wget -O - -q http://subdomain.mysite.com/cron.php",$rval);
       break;
}
$ctrnum = (int) $croncounter;
$ctrnum++;
if ($ctrnum > 1) $ctrnum = 0;
rewind($cfhandle);
$croncounter = (string) $ctrnum;
fputs($cfhandle,$croncounter,2);
fclose($cfhandle);

This code works well. There is only one problem that I've not been able to solve. Normally, when I've used wgetin the system cron I will only get a notification per email when an error occurs. With multicron I recieve an email notification on every cron call. The email has no contents and there was no error, since the cron job was called successfully. This is a bit irritating to say the least.

Thus, the new PHP script must be producing some kind of output that the system cron deems worthy of notification. But what? I've tried all variations of the system call and have also tried passthru() and exec() but each one has the same behaviour. Does anyone have an idea about how to eliminiate this problem?

Thanks

Comments

ap’s picture

I noticed that the official Drupal Cron.php is missing the closing php tag "?>". I thought I try that out on my script and viola it works!!! For some reason, leaving off the closing php tag does not produce any output and the cron-daemon does not send an email for every cron call (assuming there are no errors).

So there you have it. With this script you can multiply your cron jobs, so that in situations like mine (multisite only one cron job) you can still automaticaly call cron for all the sites.

<?php
/*Read Write length of 2 limits subdomains to 99 */

$cfname="multicron.dat";
if (file_exists($cfname)) {
   $cfhandle = fopen($cfname,"r+");
   $croncounter =fgets($cfhandle,2);
} else {
   $cfhandle = fopen($cfname,"w");
   $croncounter="0";
}

switch($croncounter) {
   case "0":
       $rstr = system("/usr/bin/wget -O - -q http://mysite.com/cron.php",$rval);
       break;
   case "1":
       $rstr = system("/usr/bin/wget -O - -q http://subdomain.mysite.com/cron.php",$rval);
       break;
}
$ctrnum = (int) $croncounter;
$ctrnum++;
if ($ctrnum > 1) $ctrnum = 0;
rewind($cfhandle);
$croncounter = (string) $ctrnum;
fputs($cfhandle,$croncounter,2);
fclose($cfhandle);