PHP daemon with start/stop/restart/stat function
Jump to [1] [Full screen preview]
pidfile = __DIR__ . '/' . self::PIDNAME . '.pid'; } /** * @return int */ private function daemon() { if (file_exists($this->pidfile)) { echo "The file $this->pidfile exists.\n"; exit(); } $pid = pcntl_fork(); if ($pid == -1) { die('could not fork'); } else if ($pid) { // we are the parent exit($pid); } else { // we are the child file_put_contents($this->pidfile, getmypid()); posix_setuid(self::uid); posix_setgid(self::gid); cli_set_process_title(self::PROCESS_NAME); pcntl_signal(SIGHUP, [$this, 'signoH']); pcntl_signal(SIGTERM, [$this, 'signoH']); pcntl_signal(SIGCHLD, [$this, 'signoH']); pcntl_signal(SIGQUIT, [$this, 'signoH']); pcntl_signal(SIGINT, [$this, 'signoH']); pcntl_signal(SIGUSR1, [$this, 'signoH']); return (getmypid()); } } /** * */ private function run() { do { pcntl_signal_dispatch(); if ($this->stop) { break; } echo "I am alive" . mt_rand(0,20) . "...\n"; sleep(5); } while (true); echo ("进程退出\n"); } public function restart() { $this->stop(); $this->start(); print "重启成功!\n"; } /** * */ private function start() { $pid = $this->daemon(); $this->run(); } /** * */ private function stop() { if (file_exists($this->pidfile)) { $pid = file_get_contents($this->pidfile); posix_kill($pid, SIGKILL); unlink($this->pidfile); } } /** * @param $proc */ private function help($proc) { printf("%s start | stop | restart | stat | help \n", $proc); } /** * @param $argv */ public function main($argv) { if (count($argv) < 2) { printf("please input help parameter\n"); exit(); } if ($argv[1] === 'stop') { $this->stop(); } else if ($argv[1] === 'start') { $this->start(); } else if ($argv[1] === 'restart') { $this->restart(); } else if ($argv[1] === 'stat') { if (is_file($this->pidfile)) { posix_kill(file_get_contents($this->pidfile), SIGHUP); } else { print "\n_______程序没有启动________\n"; } } else { $this->help("command list :"); } } /** * @param $instance * @param $channelName * @param $message */ public function handle($instance, $channelName, $message) { file_put_contents(__DIR__ . "/$channelName.txt", $message . "\n", FILE_APPEND); } /** * @param $signo */ public function signoH($signo) { switch ($signo) { case SIGHUP : print "\n___________运行状态___________\n"; print "HOST :" . self::HOST . "\n"; print "PORT :" . self::PORT . "\n"; print "NAME : " . self::PROCESS_NAME . "\n"; print "PID : " . file_get_contents($this->pidfile) . "\n"; print "________________________________\n"; break; case SIGTERM: posix_kill(file_get_contents($this->pidfile), 9); break; default : print "\n________________________________\n"; print "呀!~有人想杀掉我!\n"; print "________________________________\n"; } } }
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
2 weeks ago
By DDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
CakePHP Tutorial
1376
52

