diff --git a/includes/common.inc b/includes/common.inc
index 3b4bf58..94f101e 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -92,6 +92,11 @@ define('JS_THEME', 100);
 define('HTTP_REQUEST_TIMEOUT', -1);
 
 /**
+ * Lock time for cronjobs with time limit 0.
+ */
+define('DRUPAL_CRON_NO_TIMELIMIT_LOCK_TIME', 999999);
+
+/**
  * @defgroup block_caching Block Caching
  * @{
  * Constants that define each block's caching state.
@@ -5237,8 +5242,20 @@ 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.
+  $lockTime = ini_get('max_execution_time');
+
+  // In case the execution time is endless, we use a very high
+  // lock time.
+  if ($lockTime == 0) {
+    $lockTime = DRUPAL_CRON_NO_TIMELIMIT_LOCK_TIME;
+  }
 
   $return = FALSE;
   // Grab the defined cron queues.
@@ -5246,7 +5263,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 +5331,9 @@ function drupal_cron_cleanup() {
 
     // Release cron semaphore.
     variable_del('cron_semaphore');
+
+    // Release cron lock.
+    lock_release('cron');
   }
 }
 
