From e7f95dbe999e9ab73566237a2863bb97c0301f0c Mon Sep 17 00:00:00 2001 From: Kordian Bruck Date: Mon, 3 Dec 2018 18:53:20 +0100 Subject: [PATCH] Warn if the lockfile for system cron is older than a day We want to avoid deadlock situations and right now if there is a process running with the same PID that is not our process (e.g. after a reboot) we might be waiting forever to run the cron again. So to make this transparent to the user, we at least post a warning into the logfile. --- server/cron.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/server/cron.php b/server/cron.php index 261abd983b..6f74bd3570 100644 --- a/server/cron.php +++ b/server/cron.php @@ -32,9 +32,18 @@ define('SCRIPT_PATH', dirname($_SERVER["SCRIPT_FILENAME"])); require SCRIPT_PATH."/lib/config.inc.php"; // Check whether another instance of this script is already running -if (is_file($conf['temppath'] . $conf['fs_div'] . '.ispconfig_cron_lock')) { +$lockFile = $conf['temppath'] . $conf['fs_div'] . '.ispconfig_cron_lock'; +if (is_file($lockFile)) { clearstatcache(); - $pid = trim(file_get_contents($conf['temppath'] . $conf['fs_div'] . '.ispconfig_cron_lock')); + + // Maybe we hit a deadlock and the lock file is no longer relevant + if(filemtime($lockFile) > time() - 86400) { // 86400 seconds = 1 day + if($conf['log_priority'] <= LOGLEVEL_WARN) print @date('d.m.Y-H:i').' - WARNING - The cron lock file is older than one day.' . "\n"; + exit; + } + + // Check if the process id we have in the lock file is still present + $pid = trim(file_get_contents($lockFile)); if(preg_match('/^[0-9]+$/', $pid)) { if(file_exists('/proc/' . $pid)) { if($conf['log_priority'] <= LOGLEVEL_WARN) print @date('d.m.Y-H:i').' - WARNING - There is already an instance of server.php running with pid ' . $pid . '.' . "\n"; @@ -45,7 +54,7 @@ if (is_file($conf['temppath'] . $conf['fs_div'] . '.ispconfig_cron_lock')) { } // Set Lockfile -@file_put_contents($conf['temppath'] . $conf['fs_div'] . '.ispconfig_cron_lock', getmypid()); +@file_put_contents($lockFile, getmypid()); if($conf['log_priority'] <= LOGLEVEL_DEBUG) print 'Set Lock: ' . $conf['temppath'] . $conf['fs_div'] . '.ispconfig_cron_lock' . "\n"; -- GitLab