简体   繁体   English

从php脚本执行linux命令

[英]execute linux commands from php scripts

Good day to all. 祝大家有美好的一天。

I need to send a request to a script and then to execute some commands (basically I need to start ffmpeg when clicking on a link). 我需要向脚本发送请求,然后执行一些命令(基本上,单击链接时,我需要启动ffmpeg)。

I tried this: 我尝试了这个:

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

and this 和这个

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

In both cases I got timeout. 在这两种情况下,我都超时。 Can any1 help me with this? 任何人都可以帮我吗? Thank you. 谢谢。

You could try this, its has been posted by user bahri at bahri dot info as a comment to the PHP manual entry for exec 您可以尝试一下,它已经由用户bahri发布在bahri点信息上,作为对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);
    }
?>

If this is only a timeout error, try putting set_time_limit(xx); 如果这只是超时错误,请尝试放置set_time_limit(xx); on top of your code. 在您的代码之上。 With xx corresponding to the time to wait in seconds. 与xx对应的等待时间以秒为单位。

Putting 0 means no time limit, but it may be endless if your script enters an infinite loop, of if it is waiting a feedback from your encoding command that never arrives... 放置0表示没有时间限制,但是如果您的脚本进入无限循环,则可能是无止境的,例如它正在等待从未到达的编码命令的反馈...

尝试增加php.ini文件中的PHP脚本执行时间或通过设置功能set_time_limit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM