From cb80728720f5582af4101a80516666aa5b0ba896 Mon Sep 17 00:00:00 2001
From: Lars Lenecke <lars@eatsmarter.de>
Date: Tue, 28 Jan 2014 13:41:13 +0100
Subject: [PATCH] Fixed cron job execution with PHPCLI
---
 includes/common.inc | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/includes/common.inc b/includes/common.inc
index 3b4bf58..8f93857 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -5237,8 +5237,21 @@ function drupal_cron_run() {
   $original_user = $GLOBALS['user'];
   $GLOBALS['user'] = drupal_anonymous_user();
 
-  // Try to allocate enough time to run all the hook_cron implementations.
-  drupal_set_time_limit(240);
+  // Try to allocate enough time to run all the hook_cron implementations if
+  // needed and possible.
+  if (ini_get('max_execution_time') != 0 && ini_get('max_execution_time') < 240) {
+    drupal_set_time_limit(240);
+  }
+  // Get currently set time limit as lock time.
+  else {
+    $lockTime = ini_get('max_execution_time');
+
+    // In case the execution time is endless, we use a very high
+    // lock time.
+    if ($lockTime == 0) {
+      $lockTime = 99999;
+    }
+  }
 
   $return = FALSE;
   // Grab the defined cron queues.
@@ -5246,7 +5259,7 @@ function drupal_cron_run() {
   drupal_alter('cron_queue_info', $queues);
 
   // Try to acquire cron lock.
-  if (!lock_acquire('cron', 240.0)) {
+  if (!lock_acquire('cron', $lockTime)) {
     // Cron is still running normally.
     watchdog('cron', 'Attempting to re-run cron while it is already running.', array(), WATCHDOG_WARNING);
   }
@@ -5314,6 +5327,9 @@ function drupal_cron_cleanup() {
 
     // Release cron semaphore.
     variable_del('cron_semaphore');
+
+    // Release cron lock.
+    lock_release('cron');
   }
 }
 
-- 
1.8.3.4 (Apple Git-47)

