Index: common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.954
diff -u -r1.954 common.inc
--- includes/common.inc	5 Aug 2009 15:58:34 -0000	1.954
+++ includes/common.inc	5 Aug 2009 20:36:12 -0000
@@ -3587,19 +3587,16 @@
   // Allow execution to continue even if the request gets canceled.
   @ignore_user_abort(TRUE);
 
-  // Try to allocate enough time to run all the hook_cron implementations.
-  drupal_set_time_limit(240);
-
   // Fetch the cron semaphore
   $semaphore = variable_get('cron_semaphore', FALSE);
 
   if ($semaphore) {
     if (REQUEST_TIME - $semaphore > 3600) {
-      // Either cron has been running for more than an hour or the semaphore
+      // Either cron has been running for more than an hour or the semaphore.
       // was not reset due to a database error.
       watchdog('cron', 'Cron has been running for more than an hour and is most likely stuck.', array(), WATCHDOG_ERROR);
 
-      // Release cron semaphore
+      // Release cron semaphore.
       variable_del('cron_semaphore');
     }
     else {
@@ -3608,23 +3605,35 @@
     }
   }
   else {
-    // Register shutdown callback
+    // Register shutdown callback.
     register_shutdown_function('drupal_cron_cleanup');
 
-    // Lock cron semaphore
+    // Lock cron semaphore.
     variable_set('cron_semaphore', REQUEST_TIME);
 
-    // Iterate through the modules calling their cron handlers (if any):
-    module_invoke_all('cron');
-
-    // Record cron time
+    // We will not set a limit if allocated time is already unlimited, it
+    // would limit environement such as shell scripts.
+    if ((int) ini_get('max_execution_time') > 0) {
+      // Iterate through the modules implementing a cron hook.
+      foreach (module_implements('cron') as $module) {
+        // Try to allocate enough time to run the current cron implementation.
+        drupal_set_time_limit(30);
+        call_user_func_array($module . '_cron',array());
+      }
+    }
+    else {
+      // Iterate through the modules and calling their cron implementation.
+      module_invoke_all('cron');
+    }
+    
+    // Record cron time.
     variable_set('cron_last', REQUEST_TIME);
     watchdog('cron', 'Cron run completed.', array(), WATCHDOG_NOTICE);
 
-    // Release cron semaphore
+    // Release cron semaphore.
     variable_del('cron_semaphore');
 
-    // Return TRUE so other functions can check if it did run successfully
+    // Return TRUE so other functions can check if it did run successfully.
     return TRUE;
   }
 }

