🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## linux demo <details> <summary>demo.php</summary> ``` <?php /** * 只支持linux */ class Process{ private $pid; private $command; public $errorMsg =''; private $pidFile='process.pid'; /** * Process constructor. * @param string $pidfile 每个 file 文件存储一个 后台命令 */ public function __construct($pidfile=''){ if($pidfile) { $this->pidFile = $pidfile; } //设置获取pid $this->getPidByFile(); } private function runCom(){ $command = 'nohup '.$this->command.' > /dev/null 2>&1 & echo $!'; exec($command ,$op); if(!$op[0]){ $this->errorMsg="runCom 后无法获取 pid ".json_encode($op,JSON_UNESCAPED_UNICODE); return false; } $this->pid = (int)$op[0]; if(!$this->savePidByFile()){ $this->errorMsg="pid 无法保存到文件"; return false; } return true; } private function setPid($pid){ $this->pid = $pid; } public function getPid(){ return $this->pid; } public function status(){ $command = 'ps -p '.$this->pid; exec($command,$op); if (!$op[1]){ return false; } return true; } public function start($cmd=''){ if(!$cmd) { $this->errorMsg="运行指令为空"; return false; } $this->command=$cmd; if($this->status()){ return true; //已启动 } if(!$this->runCom()){ return false; } return true; } public function stop(){ if(!$this->pid) { $this->errorMsg="无法找到 pid 程序已经停止"; return false; } $command = 'kill -9 '.$this->pid; exec($command); if ($this->status() !== false){ return false; } if(!$this->delPidByFile()){ $this->errorMsg="删除pid失败"; return false; } return true; } private function savePidByFile(){ return file_put_contents($this->pidFile, $this->getPid())!==false; } private function getPidByFile(){ $pid = file_get_contents($this->pidFile); $this->setPid($pid); } private function delPidByFile(){ return file_put_contents($this->pidFile,'')!==false; } } $type = (int)$_GET['type'];// type 0=停止,1=开始 2=重启启动 3 查看状态 $process = (new Process("process.pid")); $cmd = "php -S 0.0.0.0:8889"; switch($type){ case 0: //可能已经停止,无效判断停止失败 $process->stop(); print_log("stop success"); break; case 1: if(!$process->start($cmd)){ print_log("start failed " .$process->errorMsg); } print_log("start success"); break; case 2: //可能已经停止,无效判断停止失败 $process->stop(); if(!$process->start($cmd)){ print_log("start failed " .$process->errorMsg); } print_log("restart success"); break; case 3: if($process->status()){ print_log("running"); }else{ print_log("stop !"); } break; } function print_log($msg){ echo $msg; } ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <p>服务状态: <?php echo $process->status()?"running":"stop" ;?></p> <ul> <li><a href="?type=0">停止服务器</a></li> <li><a href="?type=1">开启服务器</a></li> <li><a href="?type=2">重启服务</a></li> </ul> </body> </html> ``` </details> <br/> 运行 ``` // 再linux 环境中 > php -S 0.0.0.0:8888 ``` 访问浏览器 http://127.0.0.01:8888,即可控制程序启停 ![](https://img.kancloud.cn/3b/1b/3b1bec3a890d7d7abc8dd580f27f1f15_195x173.png) ## windw demo 兼容window的 启动进程,需要修改启动,查询和停止的代码 **启动** ``` pclose(popen('start /B '.$this->command, 'r')); $pid_cmd = sprintf("tasklist /fi \"imagename eq %s.exe\"", explode(' ', $this->command)[0]); //$shell_exec 输出 // //映像名称 PID 会话名 会话# 内存使用 //========================= ======== ================ =========== ============ //python.exe 21972 Console 1 8,580 K $shell_exec = shell_exec($pid_cmd); $explode = array_values(array_filter(explode("\n", $shell_exec))); if(empty($explode[2])){ throw new \RuntimeException("run_1 后无法获取 pid "); } $explode1 = array_values(array_filter(explode(" ", $explode[2]))); if(empty($explode1[1])){ throw new \RuntimeException("run_2 后无法获取 pid "); } $pid = $explode1[1]; // 得到pid ``` **查询状态** ``` $command = sprintf("tasklist /fi \"PID eq %s\"",$pid); $shell_exec = shell_exec($command); // 输出内容中有pid if(!strpos($shell_exec,$pid)){ return false; } ``` **停止** ``` $command = sprintf("taskkill /F /PID %s",$pid); exec($command); if($this->status() !== false){ return false; } ```