<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云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; } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看