The Graphic Design Shop is a full service provider of Website Design and Graphic Design. We would be happy to assist you with everything from web design and web development to Logo Design and Promotion. If you're in business, whether you need a website or a full corporate identity, The Graphic Design Shop can help you increase your company's visibility and improve its graphic image.

Run more than 5 Cron Jobs on Media Temple Grid Hosting

I’ve been very happy with Media Temple thus far and the Grid Hosting that they provide. I did however run into a bit of frustration when I discovered that they only allow up to five cron jobs. So I did what any designer would do and found a developer!

Enter Graham Dawson. Graham is an Australian Web/IPhone Developer that found time in his busy schedule to help me out on this problem.

Basically, Graham designed a master cron file that housed the individual cron runs.

So without further adieu, this is how you run more than 5 Cron Jobs on Media Temple Grid Hosting.

I will first break this solution down into three parts.

Part 1 – Beginning and recording the start of the Cron Job

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
 // This is generic scheduler for multiple tasks via one cron job
 
  echo 'Running CRON job: ' . basename(__FILE__) . ' ' . date(DATE_RFC822) . '<br \>';
 
  // Need to know HOW OFTEN it runs
  // ASSUME frequency is based on cycle start at 0 mins of each hour
  // ====================
  $RunFrequencyMins = 60;
 
 
  // ====================
 
  function timeStamp()
  {
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
  }
 
  $startTime = timeStamp();
 
  set_time_limit(600); // Allow sub-jobs to execute for up to 10 minutes each
 
  // Current directory needs to be set to where this script is executing
 
  // Should now be just past the minute
  $now1 = getdate();
  $hrs1 = $now1['hours'];
  $mins1 = $now1['minutes'];
  $secs1 = $now1['seconds'];

You should be able to use this bit of code to link directly to your cron file though the appropriate directories.

Part 1.5 – Use this code if you are scheduling cron jobs for Non-Drupal sites

1
2
3
4
5
6
7
// Cron job #1 - Run once per day at 1 am only
  chdir(dirname(__FILE__));
  if ($hrs1 == 1) {
    if ($mins1 < 2) {
      include('../domains/yoursite.com/html/cron.php');
    }
  }

I was not able to do so and I believe the reason is Drupal related. If you want to explore why, check this comment in a Drupal guide.

Since that didn’t work, Graham helped me create this bit of code that called the individual site’s cron file using the URL.

You will use this code over and over for each individual site that you want to create a cron job for.

Part 2 – Calling individual cron jobs

1
2
3
4
5
6
7
8
9
10
  // Cron job #1 - Run once per day at 2 pm only
  chdir(dirname(__FILE__));
  if ($hrs1 == 14) {
    if ($mins1 < 2) {
      $url = 'http://yoursite.com/cron.php';
      $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_exec($ch);
    }
  }

For each site, change the time (represented here by 14 or 2pm).

When you have all your sites entered, add the closing code.

Part 3 – Finishing and recording the end of the Cron Job

1
2
3
4
5
echo 'Finished CRON job: ' . date(DATE_RFC822) . '<br \>';
  $endTime = timeStamp();
  $elapsedTime = round($endTime - $startTime, 2);
  echo 'Elapsed time = ' . $elapsedTime . ' secs' . '<br \>';
?>

Put all this code into one file and name it newcron.php. FTP this new file in the data directory of you hosting account. (/home/######/data/)

The only thing left to do is set up our master cron job via Media Temple.

Add a new cron job and input this command: php5 /home/######/data/newcron.php

Thanks again to Graham Dawson:

He has a B.Sc. in Physics and Meteorology, and a Ph.D. in Dynamic Coastal Oceanography.

Twitter: http://twitter.com/gpdawson
URL: http://www.ajnaware.com/

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Twitter
  • Reddit
This entry was posted in Blog, Tutorials. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared.