cron.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  4. * @author Christopher Schäpers <kondou@ts.unde.re>
  5. * @author Jakob Sack <mail@jakobsack.de>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Oliver Kohl D.Sc. <oliver@kohl.bz>
  9. * @author Robin Appelman <icewind@owncloud.com>
  10. * @author Steffen Lindner <mail@steffen-lindner.de>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Vincent Petry <pvince81@owncloud.com>
  13. *
  14. * @copyright Copyright (c) 2015, ownCloud, Inc.
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. try {
  31. require_once 'lib/base.php';
  32. if (\OCP\Util::needUpgrade()) {
  33. \OCP\Util::writeLog('cron', 'Update required, skipping cron', \OCP\Util::DEBUG);
  34. exit;
  35. }
  36. if (\OC::$server->getSystemConfig()->getValue('maintenance', false)) {
  37. \OCP\Util::writeLog('cron', 'We are in maintenance mode, skipping cron', \OCP\Util::DEBUG);
  38. exit;
  39. }
  40. if (\OC::$server->getSystemConfig()->getValue('singleuser', false)) {
  41. \OCP\Util::writeLog('cron', 'We are in admin only mode, skipping cron', \OCP\Util::DEBUG);
  42. exit;
  43. }
  44. // load all apps to get all api routes properly setup
  45. OC_App::loadApps();
  46. \OC::$server->getSession()->close();
  47. // initialize a dummy memory session
  48. \OC::$server->setSession(new \OC\Session\Memory(''));
  49. $logger = \OC_Log::$object;
  50. // Don't do anything if ownCloud has not been installed
  51. if (!OC_Config::getValue('installed', false)) {
  52. exit(0);
  53. }
  54. \OC::$server->getTempManager()->cleanOld();
  55. // Exit if background jobs are disabled!
  56. $appMode = \OCP\BackgroundJob::getExecutionType();
  57. if ($appMode == 'none') {
  58. if (OC::$CLI) {
  59. echo 'Background Jobs are disabled!' . PHP_EOL;
  60. } else {
  61. OC_JSON::error(array('data' => array('message' => 'Background jobs disabled!')));
  62. }
  63. exit(1);
  64. }
  65. if (OC::$CLI) {
  66. // set to run indefinitely if needed
  67. set_time_limit(0);
  68. // the cron job must be executed with the right user
  69. if (!OC_Util::runningOnWindows()) {
  70. if (!function_exists('posix_getuid')) {
  71. echo "The posix extensions are required - see http://php.net/manual/en/book.posix.php" . PHP_EOL;
  72. exit(0);
  73. }
  74. $user = posix_getpwuid(posix_getuid());
  75. $configUser = posix_getpwuid(fileowner(OC::$SERVERROOT . '/config/config.php'));
  76. if ($user['name'] !== $configUser['name']) {
  77. echo "Console has to be executed with the same user as the web server is operated" . PHP_EOL;
  78. echo "Current user: " . $user['name'] . PHP_EOL;
  79. echo "Web server user: " . $configUser['name'] . PHP_EOL;
  80. exit(0);
  81. }
  82. }
  83. $config = OC::$server->getConfig();
  84. $instanceId = $config->getSystemValue('instanceid');
  85. $lockFileName = 'owncloud-server-' . $instanceId . '-cron.lock';
  86. $lockDirectory = $config->getSystemValue('cron.lockfile.location', sys_get_temp_dir());
  87. $lockDirectory = rtrim($lockDirectory, '\\/');
  88. $lockFile = $lockDirectory . '/' . $lockFileName;
  89. if (!file_exists($lockFile)) {
  90. touch($lockFile);
  91. }
  92. // We call ownCloud from the CLI (aka cron)
  93. if ($appMode != 'cron') {
  94. \OCP\BackgroundJob::setExecutionType('cron');
  95. }
  96. // open the file and try to lock if. If it is not locked, the background
  97. // job can be executed, otherwise another instance is already running
  98. $fp = fopen($lockFile, 'w');
  99. $isLocked = flock($fp, LOCK_EX|LOCK_NB, $wouldBlock);
  100. // check if backgroundjobs is still running. The wouldBlock check is
  101. // needed on systems with advisory locking, see
  102. // http://php.net/manual/en/function.flock.php#45464
  103. if (!$isLocked || $wouldBlock) {
  104. echo "Another instance of cron.php is still running!" . PHP_EOL;
  105. exit(1);
  106. }
  107. // Work
  108. $jobList = \OC::$server->getJobList();
  109. $jobs = $jobList->getAll();
  110. foreach ($jobs as $job) {
  111. $job->execute($jobList, $logger);
  112. }
  113. // unlock the file
  114. flock($fp, LOCK_UN);
  115. fclose($fp);
  116. } else {
  117. // We call cron.php from some website
  118. if ($appMode == 'cron') {
  119. // Cron is cron :-P
  120. OC_JSON::error(array('data' => array('message' => 'Backgroundjobs are using system cron!')));
  121. } else {
  122. // Work and success :-)
  123. $jobList = \OC::$server->getJobList();
  124. $job = $jobList->getNext();
  125. if ($job != null) {
  126. $job->execute($jobList, $logger);
  127. $jobList->setLastJob($job);
  128. }
  129. OC_JSON::success();
  130. }
  131. }
  132. // Log the successful cron execution
  133. if (\OC::$server->getConfig()->getSystemValue('cron_log', true)) {
  134. \OC::$server->getConfig()->setAppValue('core', 'lastcron', time());
  135. }
  136. exit();
  137. } catch (Exception $ex) {
  138. \OCP\Util::writeLog('cron', $ex->getMessage(), \OCP\Util::FATAL);
  139. }