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.

101 lines
1.9KB

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