如何用PHP编写实现守护进程的创建、启动和停止功能?

2026-06-10 02:164阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计708个文字,预计阅读时间需要3分钟。

如何用PHP编写实现守护进程的创建、启动和停止功能?

原文示例讲述了一个PHP实现简单守护进程创建、启动与关闭操作的实例。以下是修改后的内容:

---

本例展示了如何使用PHP实现简单的守护进程操作:创建、启动与关闭。供家长参考,内容如下:

前提条件:需安装PCNTL扩展,可通过以下命令检查是否已安装:php -m | grep pcntl

代码示例:phpclass Daemon { private $pidfile;

function __construct($pidfile) { $this->pidfile=$pidfile; }

function start() { // 创建进程并获取进程ID $pid=pcntl_fork(); if ($pid==-1) { // 创建失败 exit(无法创建守护进程。\n); } elseif ($pid) { // 父进程 echo 守护进程已启动,PID: $pid\n; exit; } else { // 子进程 $this->daemonize(); // 守护进程代码 } }

function daemonize() { // 获取当前进程ID $pid=pcntl_getpid(); // 写入PID到文件 file_put_contents($this->pidfile, $pid); // 与控制终端断开连接 $this->closeParent(); // 获取新的会话 $this->startNewSession(); // 重置工作目录 chdir(/); // 关闭文件描述符 $this->closeFDs(); // 不处理SIGCHLD信号 pcntl_signal(SIGCHLD, SIG_IGN); }

private function closeParent() { // 关闭标准输入、输出、错误 fclose(STDIN); fclose(STDOUT); fclose(STDERR); }

private function startNewSession() { // 创建新的会话 posix_setsid(); }

如何用PHP编写实现守护进程的创建、启动和停止功能?

private function closeFDs() { // 关闭所有文件描述符 for ($i=0; $i <64; $i++) { fclose(fopen(/dev/null, w)); } }

function stop() { // 读取PID $pid=file_get_contents($this->pidfile); if ($pid) { // 杀死进程 $this->killProcess($pid); // 删除PID文件 unlink($this->pidfile); echo 守护进程已停止。\n; } else { echo 没有找到守护进程。\n; } }

private function killProcess($pid) { // 杀死进程 pcntl_signal(SIGTERM, SIG_IGN); pcntl_waitpid($pid, $status); }}

使用示例:php$daemon=new Daemon('/tmp/daemon.pid');$daemon->start();sleep(10); // 等待一段时间$daemon->stop();

---

注意:以上代码仅供参考,实际使用时可能需要根据具体情况进行调整。

本文实例讲述了php实现简单的守护进程创建、开启与关闭操作。分享给大家供大家参考,具体如下:

前提要安装有pcntl扩展,可通过php -m查看是否安装

<?php class Daemon { private $pidfile; function __construct() { $this->pidfile = dirname(__FILE__).'/daemontest.pid'; } private function startDeamon() { 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) { echo 'start ok'; exit($pid); } else { // we are the child file_put_contents($this->pidfile, getmypid()); return getmypid(); } } private function start(){ $pid = $this->startDeamon(); while (true) { file_put_contents(dirname(__FILE__).'/test.txt', date('Y-m-d H:i:s'), FILE_APPEND); sleep(2); } } private function stop(){ if (file_exists($this->pidfile)) { $pid = file_get_contents($this->pidfile); posix_kill($pid, 9); unlink($this->pidfile); } } public function run($argv) { if($argv[1] == 'start') { $this->start(); }else if($argv[1] == 'stop') { $this->stop(); }else{ echo 'param error'; } } } $deamon = new Daemon(); $deamon->run($argv);

启动

php deamon.php start

关闭

php deamon.php stop

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP进程与线程操作技巧总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

本文共计708个文字,预计阅读时间需要3分钟。

如何用PHP编写实现守护进程的创建、启动和停止功能?

原文示例讲述了一个PHP实现简单守护进程创建、启动与关闭操作的实例。以下是修改后的内容:

---

本例展示了如何使用PHP实现简单的守护进程操作:创建、启动与关闭。供家长参考,内容如下:

前提条件:需安装PCNTL扩展,可通过以下命令检查是否已安装:php -m | grep pcntl

代码示例:phpclass Daemon { private $pidfile;

function __construct($pidfile) { $this->pidfile=$pidfile; }

function start() { // 创建进程并获取进程ID $pid=pcntl_fork(); if ($pid==-1) { // 创建失败 exit(无法创建守护进程。\n); } elseif ($pid) { // 父进程 echo 守护进程已启动,PID: $pid\n; exit; } else { // 子进程 $this->daemonize(); // 守护进程代码 } }

function daemonize() { // 获取当前进程ID $pid=pcntl_getpid(); // 写入PID到文件 file_put_contents($this->pidfile, $pid); // 与控制终端断开连接 $this->closeParent(); // 获取新的会话 $this->startNewSession(); // 重置工作目录 chdir(/); // 关闭文件描述符 $this->closeFDs(); // 不处理SIGCHLD信号 pcntl_signal(SIGCHLD, SIG_IGN); }

private function closeParent() { // 关闭标准输入、输出、错误 fclose(STDIN); fclose(STDOUT); fclose(STDERR); }

private function startNewSession() { // 创建新的会话 posix_setsid(); }

如何用PHP编写实现守护进程的创建、启动和停止功能?

private function closeFDs() { // 关闭所有文件描述符 for ($i=0; $i <64; $i++) { fclose(fopen(/dev/null, w)); } }

function stop() { // 读取PID $pid=file_get_contents($this->pidfile); if ($pid) { // 杀死进程 $this->killProcess($pid); // 删除PID文件 unlink($this->pidfile); echo 守护进程已停止。\n; } else { echo 没有找到守护进程。\n; } }

private function killProcess($pid) { // 杀死进程 pcntl_signal(SIGTERM, SIG_IGN); pcntl_waitpid($pid, $status); }}

使用示例:php$daemon=new Daemon('/tmp/daemon.pid');$daemon->start();sleep(10); // 等待一段时间$daemon->stop();

---

注意:以上代码仅供参考,实际使用时可能需要根据具体情况进行调整。

本文实例讲述了php实现简单的守护进程创建、开启与关闭操作。分享给大家供大家参考,具体如下:

前提要安装有pcntl扩展,可通过php -m查看是否安装

<?php class Daemon { private $pidfile; function __construct() { $this->pidfile = dirname(__FILE__).'/daemontest.pid'; } private function startDeamon() { 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) { echo 'start ok'; exit($pid); } else { // we are the child file_put_contents($this->pidfile, getmypid()); return getmypid(); } } private function start(){ $pid = $this->startDeamon(); while (true) { file_put_contents(dirname(__FILE__).'/test.txt', date('Y-m-d H:i:s'), FILE_APPEND); sleep(2); } } private function stop(){ if (file_exists($this->pidfile)) { $pid = file_get_contents($this->pidfile); posix_kill($pid, 9); unlink($this->pidfile); } } public function run($argv) { if($argv[1] == 'start') { $this->start(); }else if($argv[1] == 'stop') { $this->stop(); }else{ echo 'param error'; } } } $deamon = new Daemon(); $deamon->run($argv);

启动

php deamon.php start

关闭

php deamon.php stop

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP进程与线程操作技巧总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。