Forker可以让php-cli进程借助nohup以守护进程的方式运行。这个Forker仅仅是让进程成为守护进程,不会复制父进程的内存。 无 ?php// 运行实例,fork 10 个进程,每个进程输出一行 Im a worker ,并保存在 /tmp/forker.log 中:// CLI命令: php Forker.php 10/
Forker 可以让 php-cli 进程借助 nohup 以守护进程的方式运行。这个 Forker 仅仅是让进程成为守护进程,不会复制父进程的内存。
<?php // 运行实例,fork 10 个进程,每个进程输出一行 Im a worker ,并保存在 /tmp/forker.log 中: // CLI命令: php Forker.php 10 /* if (empty($argv[1])) { echo "Im a worker\n"; sleep(10); exit(); } else { $forker = new Forker('/tmp/forker.log'); $forker->fork($forker->findCommand('php') . ' ' . __FILE__, (int)$argv[1] <= 0 ? 10 : (int)$argv[1]); } */ /** * Forker 可以让 php-cli 进程借助 nohup 以守护进程的方式运行。 * 这个 Forker 仅仅是让进程成为守护进程,不会复制父进程的内存。 */ class Forker { private $nohub = '/usr/bin/nohup'; private $out = '/tmp/forker.log'; /** * @param string $output 输出文件的路径。进程的标准输出将重定向到此文件 * @throws \RuntimeException */ public function __construct($output = '') { if (false !== ($nohup = $this->findCommand('nohup'))) { $this->nohub = $nohup; } if (!is_executable($this->nohub)) { throw new \RuntimeException('nohup not excutable'); } if ($output) { $this->setOutput($output); } } /** * 设置输出文件的路径。进程的标准输出将重定向到此文件 * @param string $file * @return \Forker * @throws \RuntimeException */ public function setOutput($file) { if (!is_file($file)) { $dir = dirname($file); if ((!is_dir($dir) && !mkdir($dir, 0755, true)) || !is_writable($dir)) { throw new \RuntimeException('output is not writable, can not create output'); } } else if (!is_writable($file)) { throw new \RuntimeException('output is not writable'); } $this->out = $file; return $this; } /** * 获取输出文件的路径 * @return string */ public function getOutput() { return $this->out; } /** * 执行命令 * @param string $command 命令。命令中的文件参数需要使用绝对路径 * @param int $forks fork的进程数 */ public function fork($command, $forks = 1) { for ($i = 0; $i < $forks; ++$i) { $this->execute($command); } } /** * 根据当前环境查找命令的绝对路径 * @param string $name * @return boolean */ public function findCommand($name) { $file = trim(exec("which {$name}")); if (is_file($file) && is_executable($file)) { return $file; } return false; } /** * 执行命令,成功返回 true,失败返回 false * @param string $command * @return boolean */ private function execute($command) { $lines = []; $code = 0; exec("{$this->nohub} {$command} >> {$this->out} 2>&1 &", $lines, $code); if (0 !== (int)$code) { file_put_contents($this->out, "fork {$command} FAILD[{$code}]:\n" . implode("\n", $lines) . "\n", FILE_APPEND); return false; } file_put_contents($this->out, "fork {$command} OK\n", FILE_APPEND); return true; } }
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影:貝殼謎語解決方案
1 週前ByDDD
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

記事本++7.3.1
好用且免費的程式碼編輯器

Dreamweaver CS6
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3漢化版
中文版,非常好用