0

Linux の PHP 5.3 で php スクリプト (Jobque.php) をデーモン化したいと考えています。

アイデアは、次のようにスクリプトを呼び出すことです。

php -f ./application/Model/Jobque.php start

スクリプトが shell_exec を実行して自分自身をバックグラウンドに置き、このような処理を行うよりも

nohup php -f /var/.../application/Model/Jobque.php process 2> /dev/null & echo $!

この最後のコマンドは実際にバックグラウンドでスクリプトを開始し、すべて問題ありませんが、スクリプト自体からこのコマンドを発行すると、スクリプトは実行が停止するまで待機します (決して)

これは、スクリプトをデーモンとして開始するために使用する関数です-Windows部分は機能します

public function start_daemon() {
        if (file_exists ( $this->pidfile ))
            die ( 'process is already running - process pidfile already exists -> ' . $this->pidfile );
        $cmd = 'php -f ' . __FILE__ . ' process';
        if (substr ( php_uname (), 0, 7 ) == "Windows") {
            $WshShell = new COM ( "WScript.Shell" );
            $oExec = $WshShell->Run ( "$cmd /C dir /S %windir%", 0, false );
            exec ( 'TASKLIST /NH /FO "CSV" /FI "imagename eq php.exe" /FI "cputime eq 00:00:00"', $output );
            $output = explode ( '","', $output [0] );
            $pid = $output [1];
            file_put_contents ( $this->pidfile, $pid );
        } else {
            $execstr = "nohup $cmd 2> /dev/null & echo $!";
            //echo $execstr; -- the execstr is right in itself
            $PID = shell_exec ( $execstr );
            //echo $PID; -- we never get here
            file_put_contents ( $this->pidfile, $PID );
        }
        echo ('JobQue daemon started with pidfile:' . $this->pidfile);
    }

ここで私は何を間違っていますか、そしてそれを正しく行う方法は?

4

3 に答える 3

0

次のように、Linuxでスクリプトをデーモン化するためにpcntl_forkを使用することになりました。

public function start_daemon($worker) {
        if (file_exists ( $this->get_pidfile ( $worker ) ))
            die ( 'process is already running - process pidfile already exists -> ' . $this->get_pidfile ( $worker ) . "\n" );
        $cmd = 'php -f ' . __FILE__ . ' process';
        if ($this->is_win) {
            $WshShell = new COM ( "WScript.Shell" );
            $oExec = $WshShell->Run ( "$cmd /C dir /S %windir%", 0, false );
            exec ( 'TASKLIST /NH /FO "CSV" /FI "imagename eq php.exe" /FI "cputime eq 00:00:00"', $output );
            $output = explode ( '","', $output [0] );
            $pid = $output [1];
            file_put_contents ( $this->get_pidfile ( $worker ), $pid );
            echo ('JobQue daemon started with pidfile:' . $this->get_pidfile ( $worker ) . "\n");
        } else {
            $PID = pcntl_fork ();
            if ($PID) {
                file_put_contents ( $this->get_pidfile ( $worker ), $PID );
                echo ('JobQue daemon started with pidfile:' . $this->get_pidfile ( $worker ) . "\n");
                exit (); // kill parent
            }
            posix_setsid (); // become session leader
            chdir ( "/" );
            umask ( 0 ); // clear umask
            $this->process_jobs (); //start infinite loop
        }
    }
于 2012-05-03T06:27:20.463 に答える
0

daemonize コマンドを見てみましょう: http://software.clapper.org/daemonize/index.html

または、ubuntu の「upstart」: http://upstart.ubuntu.com/

于 2013-04-17T14:43:33.067 に答える
0

見てみましょう: http://pear.php.net/package/System_Daemon

于 2012-04-26T16:15:05.603 に答える