Advertisement
Guest User

Untitled

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