Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
2.4KB

  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * @file bin/daemon.php
  5. * @brief Run the worker from a daemon.
  6. *
  7. * This script was taken from http://php.net/manual/en/function.pcntl-fork.php
  8. */
  9. function shutdown() {
  10. posix_kill(posix_getpid(), SIGHUP);
  11. }
  12. if (in_array("start", $_SERVER["argv"])) {
  13. $mode = "start";
  14. }
  15. if (in_array("stop", $_SERVER["argv"])) {
  16. $mode = "stop";
  17. }
  18. if (in_array("status", $_SERVER["argv"])) {
  19. $mode = "status";
  20. }
  21. if (!isset($mode)) {
  22. die("Please use either 'start', 'stop' or 'status'.\n");
  23. }
  24. if (empty($_SERVER["argv"][0])) {
  25. die("Unexpected script behaviour. This message should never occur.\n");
  26. }
  27. // Fetch the base directory
  28. $directory = dirname($_SERVER["argv"][0]);
  29. if (substr($directory, 0, 1) != "/") {
  30. $directory = $_SERVER["PWD"]."/".$directory;
  31. }
  32. $directory = realpath($directory."/..");
  33. @include($directory."/.htconfig.php");
  34. if (!isset($pidfile)) {
  35. die('Please specify a pid file in the variable $pidfile in the .htconfig.php. For example:'."\n".
  36. '$pidfile = "/path/to/daemon.pid";'."\n");
  37. }
  38. if (in_array($mode, array("stop", "status"))) {
  39. $pid = @file_get_contents($pidfile);
  40. if (!$pid) {
  41. die("Pidfile wasn't found. Is the daemon running?\n");
  42. }
  43. }
  44. if ($mode == "status") {
  45. if (posix_kill($pid, 0)) {
  46. die("Daemon process $pid is running.\n");
  47. }
  48. unlink($pidfile);
  49. die("Daemon process $pid isn't running.\n");
  50. }
  51. if ($mode == "stop") {
  52. posix_kill($pid, SIGTERM);
  53. unlink($pidfile);
  54. die("Worker daemon process $pid was killed.\n");
  55. }
  56. echo "Starting worker daemon.\n";
  57. if (isset($a->config['php_path'])) {
  58. $php = $a->config['php_path'];
  59. } else {
  60. $php = "php";
  61. }
  62. // Switch over to daemon mode.
  63. if ($pid = pcntl_fork())
  64. return; // Parent
  65. fclose(STDIN); // Close all of the standard
  66. fclose(STDOUT); // file descriptors as we
  67. fclose(STDERR); // are running as a daemon.
  68. register_shutdown_function('shutdown');
  69. if (posix_setsid() < 0)
  70. return;
  71. if ($pid = pcntl_fork())
  72. return; // Parent
  73. $pid = getmypid();
  74. file_put_contents($pidfile, $pid);
  75. // Now running as a daemon.
  76. while (true) {
  77. // Just to be sure that this script really runs endlessly
  78. set_time_limit(0);
  79. // Call the worker
  80. $cmdline = $php.' bin/worker.php';
  81. $executed = false;
  82. if (function_exists('proc_open')) {
  83. $resource = proc_open($cmdline . ' &', array(), $foo, $directory);
  84. if (is_resource($resource)) {
  85. $executed = true;
  86. proc_close($resource);
  87. }
  88. }
  89. if (!$executed) {
  90. exec($cmdline.' spawn');
  91. }
  92. // Now sleep for 5 minutes
  93. sleep(300);
  94. }