検索
ホームページphp教程php手册Forker 可以让 php-cli 进程借助 nohup 以守护进程的方式运行。

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

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

SecLists

SecLists

SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。