简体   繁体   English

从命令行运行PHP

[英]Running PHP from command line

I am trying to manage a queue of files waiting to be processed by ffmpeg. 我正在尝试管理等待由ffmpeg处理的文件队列。 A page is run using CRON that runs through a database of files waiting to be processed. 使用CRON运行页面,该页面在等待处理的文件数据库中运行。 The page then builds the commands and sends them to the command line using exec() . 该页面然后构建命令,并使用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. 但是,当从命令行或CRON运行PHP页面时,它将运行exec() OK,但不会返回PHP页面以继续更新数据库和其他功能。

Example: 例:

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

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

//Page continues...but not executed
$update = mysql_query("UPDATE.....");
?>

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. 从命令行运行该页面时,将使用exec()运行该命令,但是该页面的其余部分将不执行。 I think the problem may be that I am running a command using exec() in a page run from the command line. 我认为问题可能是我在从命令行运行的页面中使用exec()运行命令。

Is it possible to run a PHP page in full from the command line which includes exec() ? 是否可以从包含exec()的命令行完整运行PHP页面?

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: 不久前,我写了一篇关于在Linux上从PHP运行后台进程的文章:

<?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 &' ); ?>

您是否尝试过使用CURL?

不确定但可能是由于cron进程的外壳程序限制,如果它作为网页工作,然后将其用作网页,则设置一个调用wget wherever_your_page_is的cron作业,它将通过您的Web服务器调用,并且应该模拟您的测试。

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

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