开发者

Running PHP from command line

I am trying to manage a queue of files waiting to be processed by ffmpeg. A page is run using CRON that runs through a database of files waiting to be processed. The page then builds the commands and sends them to the command line using exec().

However, when the PHP page is run from the command line or CRON, it runs the exec() OK, but does not return to the PHP page to continue updating the database and other functions.

Example:

<?php
$cmd = "ffmpeg inpupt.mpg output.m4v";

exec($cmd . ' 2>&1', $output, $return);

//Page continues...but not executed
$update = mysql_query("UPDATE.....");开发者_StackOverflow中文版
?>

When this page is run from the command line, the command is run using exec() but then the rest of the page is not executed. I think the problem may be that I am running a command using exec() in a page run from the command line.

Is it possible to run a PHP page in full from the command line which includes exec()?

Or is there a better way of doing this?

Thank you.


I wrote an article about Running a Background Process from PHP on Linux some time ago:

<?php system( 'sh test.sh >/dev/null &' ); ?>

Notice the & operator at the end. This starts a process that returns control to the shell immediately AND CONTINUES TO RUN in the background.

More examples:

<!--
    saving standard output to a file
    very important when your process runs in background
    as this is the only way the process can error/success
-->
<?php system( 'sh test.sh >test-out.txt &' ); ?>
<!--
    saving standard output and standard error to files
    same as above, most programs log errors to standard error hence its better to capture both
-->
<?php system( 'sh test.sh >test-out.txt 2>test-err.txt &' ); ?>


Have you tried using CURL instead?


Unsure but probably thats due to the shell constraints of cron processes if it works as a web page then use it as a web page, setup a cron job that calls wget wherever_your_page_is and it will be called via your web server and should mimic your tests.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜