从php脚本执行linux命令

时间:2011-03-29 08:51:23

标签: php linux command-line command exec

大家好日子。

我需要向脚本发送请求然后执行一些命令(基本上我需要在点击链接时启动ffmpeg)。

我试过了:

exec("ffserver &");
exec("ffmpeg -i pipe.avi output.avi");
exec("mplayer -dumpstream someinput -dumpfile pipe.avi");

和这个

shell_exec("ffserver &");
shell_exec("ffmpeg -i pipe.avi output.avi");
shell_exec("mplayer -dumpstream someinput -dumpfile pipe.avi");

在这两种情况下我都超时了。 any1可以帮助我吗?谢谢。

3 个答案:

答案 0 :(得分:1)

您可以试试这个,它已由用户 bahri发布在bahri dot info ,作为对exec

的PHP手册条目的评论
<?php
  function PsExecute($command, $timeout = 60, $sleep = 2) {
        // First, execute the process, get the process ID

        $pid = PsExec($command);

        if( $pid === false )
            return false;

        $cur = 0;
        // Second, loop for $timeout seconds checking if process is running
        while( $cur < $timeout ) {
            sleep($sleep);
            $cur += $sleep;
            // If process is no longer running, return true;

           echo "\n ---- $cur ------ \n";

            if( !PsExists($pid) )
                return true; // Process must have exited, success!
        }

        // If process is still running after timeout, kill the process and return false
        PsKill($pid);
        return false;
    }

    function PsExec($commandJob) {

        $command = $commandJob.' > /dev/null 2>&1 & echo $!';
        exec($command ,$op);
        $pid = (int)$op[0];

        if($pid!="") return $pid;

        return false;
    }

    function PsExists($pid) {

        exec("ps ax | grep $pid 2>&1", $output);

        while( list(,$row) = each($output) ) {

                $row_array = explode(" ", $row);
                $check_pid = $row_array[0];

                if($pid == $check_pid) {
                        return true;
                }

        }

        return false;
    }

    function PsKill($pid) {
        exec("kill -9 $pid", $output);
    }
?>

答案 1 :(得分:1)

如果这只是一个超时错误,请尝试使用set_time_limit(xx);在你的代码之上。 xx对应于以秒为单位的等待时间。

设置0表示没有时间限制,但如果您的脚本进入无限循环,则可能无穷无尽,如果它正在等待您的编码命令中从未到达的反馈...

答案 2 :(得分:1)

尝试在php.ini文件中增加PHP脚本执行时间,或者设置函数set_time_limit

相关问题