dreamMyDream2014 2013-12-18 09:02
浏览 36
已采纳

PHP exec背景永远不会完成

I want to process my access log in php - checking some IPs, which are leeching content and so on, everything in PHP, running as CLI. I tried to make following, but it never pass exec tail -f, so, actually I can not process the data. Any help appreciated.

$log = '/var/log/lighttpd/web.org-access.log';
$pipefile = '/www/web.org/tmp/fifo.tmp';

if(file_exists($pipefile))
    if(!unlink($pipefile)) 
        die('unable to remove stale file');

umask(0);
if(!posix_mkfifo($pipefile,0777))
    die('unable to create named pipe');

exec("tail -f $log > $pipefile 2>&1 &"); //I tried nohup and so on...
//exec("varnishncsa $log > $pipefile 2>&1 &"); //will be here instead tail -f
echo "never comes here"; //never shows up

If possible, I want to do it just in PHP, no bash/tcsh scripting (I know how to do it using those).

Thanks.

  • 写回答

1条回答 默认 最新

  • duanguilin2007 2013-12-18 10:25
    关注

    If you want exec to start a background process, you will have to redirect its output.

    Quote from the manual:

    If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

    Notice the full syntax description:

    string exec ( string $command [, array &$output [, int &$return_var ]] )
    

    Source: http://www.php.net/manual/en/function.exec.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?