/** * 返回導致子進程終止其執行的數。 * @return int */ public function getTermSignal() {// 返回 子進程 終止其執行的數 $this->requireProcessIsTerminated(__FUNCTION__);// 確認 進程 函數 已經終止,否則 拋出異常 if ($this->isSigchildEnabled()) {// 確認是否 子進程 throw new \RuntimeException('This PHP has been compiled with --enable-sigchild. Term signal can not be retrieved.'); } $this->updateStatus(false);// 阻塞 非阻塞 連接池 連接等待 return $this->processInformation['termsig'];// 返回 終止 異常數據 } /** * 檢查子進程信號是否已停止 * @return bool */ public function hasBeenStopped() { $this->requireProcessIsTerminated(__FUNCTION__);// 如果停止通過 $this->updateStatus(false);// 狀態 檢查 通過 return $this->processInformation['stopped'];// 進程停止信息 } /** * 返回導致子進程停止其執行的數。 * @return int */ public function getStopSignal() { $this->requireProcessIsTerminated(__FUNCTION__); $this->updateStatus(false); return $this->processInformation['stopsig']; }// 停止的 情況 /** * 檢查是否正在運行 * @return bool */ public function isRunning() { if (self::STATUS_STARTED !== $this->status) { return false; }// 是否跑起來了 $this->updateStatus(false);// 檢測是否是關閉狀態 return $this->processInformation['running'];// 返回提示語句 } /** * 檢查是否已開始 * @return bool */ public function isStarted() { return $this->status != self::STATUS_READY;// 直接根據 啓動標誌位進行 返回檢測 } /** * 檢查是否已終止 * @return bool */ public function isTerminated() { $this->updateStatus(false);// 如果沒有關閉 return $this->status == self::STATUS_TERMINATED;// 進行 終止 檢測 } /** * 獲取當前的狀態 * @return string */ public function getStatus() { $this->updateStatus(false);// 獲取當前狀態 return $this->status;// 返回當前狀態信息 } /** * 終止進程 */ public function stop() {// 終止進程 if ($this->isRunning()) {// 是否運行 if ('\\' === DS && !$this->isSigchildEnabled()) { exec(sprintf('taskkill /F /T /PID %d 2>&1', $this->getPid()), $output, $exitCode);// 返回 linux 命令 if ($exitCode > 0) {// 如果執行狀態 throw new \RuntimeException('Unable to kill the process');// 異常情況 } } else { $pids = preg_split('/\s+/', `ps -o pid --no-heading --ppid {$this->getPid()}`);// 產看 pid 信息 foreach ($pids as $pid) {// 循環遍歷 if (is_numeric($pid)) {// 數字進程 posix_kill($pid, 9); // 殺死進程 } } } } $this->updateStatus(false);// 更新狀態 if ($this->processInformation['running']) {// 如果還在運行中 $this->close();// 關閉 } return $this->exitcode;// 返回退出代碼 } /** * 添加一行輸出 * @param string $line */ public function addOutput($line) { $this->lastOutputTime = microtime(true); $this->stdout .= $line; }// 添加輸出信息 並且 更新最後的輸出時間 /** * 添加一行錯誤輸出 * @param string $line */ public function addErrorOutput($line) { $this->lastOutputTime = microtime(true); $this->stderr .= $line;// 行信息 }// 添加一行錯誤輸出 /** * 獲取被執行的指令 * @return string */ public function getCommandLine() { return $this->commandline; }// 獲取執行的指令 /** * 設置指令 * @param string $commandline * @return self */ public function setCommandLine($commandline) { $this->commandline = $commandline; return $this; }// 設置指令 /** * 獲取超時時間 * @return float|null */ public function getTimeout() { return $this->timeout; }// 獲取超時 時間 /** * 獲取idle超時時間 * @return float|null */ public function getIdleTimeout() { return $this->idleTimeout; }// 獲取 idle 超時時間 /** * 設置超時時間 * @param int|float|null $timeout * @return self */ public function setTimeout($timeout) { $this->timeout = $this->validateTimeout($timeout); return $this; }// 設置 超時 時間 /** * 設置idle超時時間 * @param int|float|null $timeout * @return self */ public function setIdleTimeout($timeout) { if (null !== $timeout && $this->outputDisabled) { throw new \LogicException('Idle timeout can not be set while the output is disabled.'); } $this->idleTimeout = $this->validateTimeout($timeout); return $this; }// 設置 idle 超時 時間 /** * 設置TTY * @param bool $tty * @return self */ public function setTty($tty) { if ('\\' === DS && $tty) {// 如果是 \\ 並且 需要設置 tty 這樣的話,證明在 window平臺下 throw new \RuntimeException('TTY mode is not supported on Windows platform.'); } if ($tty && (!file_exists('/dev/tty') || !is_readable('/dev/tty'))) {// tty 必須可以讀取 throw new \RuntimeException('TTY mode requires /dev/tty to be readable.'); } $this->tty = (bool)$tty;// 把當前的 tty 進行 數據類型強制轉化 return $this;// 返回當前類 } /** * 檢查是否是tty模式 * @return bool */ public function isTty()// 是否 tty 模式 { return $this->tty; } /** * 設置pty模式 * @param bool $bool * @return self */ public function setPty($bool)// 設置 pty 模式 { $this->pty = (bool)$bool; return $this; } /** * 是否是pty模式 * @return bool */ public function isPty()// 是否 pty 模式 { return $this->pty; } /** * 獲取工作目錄 * @return string|null */ public function getWorkingDirectory()// 獲取當前工作目錄, 讓我想起來了 linux pwd 命令 { if (null === $this->cwd) { return getcwd() ?: null; } return $this->cwd; } /** * 設置工作目錄 * @param string $cwd * @return self */ public function setWorkingDirectory($cwd)// 設置目錄 { $this->cwd = $cwd; return $this; } /** * 獲取環境變量 * @return array */ public function getEnv()// 獲取環境變量 { return $this->env; } /** * 設置環境變量 * @param array $env * @return self */ public function setEnv(array $env)// 設置環境變量 { $env = array_filter($env, function ($value) { return !is_array($value); }); $this->env = []; foreach ($env as $key => $value) { $this->env[(binary)$key] = (binary)$value; } return $this; }// 過濾參數 並且 設置 數據
[李景山php]每天TP5-20170124|thinkphp5-Process.php-6
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.