[英]Running PHP from command line
我正在尝试管理等待由ffmpeg处理的文件队列。 使用CRON运行页面,该页面在等待处理的文件数据库中运行。 该页面然后构建命令,并使用exec()
将它们发送到命令行。
但是,当从命令行或CRON运行PHP页面时,它将运行exec()
OK,但不会返回PHP页面以继续更新数据库和其他功能。
例:
<?php
$cmd = "ffmpeg inpupt.mpg output.m4v";
exec($cmd . ' 2>&1', $output, $return);
//Page continues...but not executed
$update = mysql_query("UPDATE.....");
?>
从命令行运行该页面时,将使用exec()
运行该命令,但是该页面的其余部分将不执行。 我认为问题可能是我在从命令行运行的页面中使用exec()
运行命令。
是否可以从包含exec()
的命令行完整运行PHP页面?
还是有更好的方法呢?
谢谢。
不久前,我写了一篇关于在Linux上从PHP运行后台进程的文章:
<?php system( 'sh test.sh >/dev/null &' ); ?>
注意最后的&
运算符。 这将启动一个过程,该过程可立即将控制权返回给外壳程序并继续在后台运行。
更多示例:
<!--
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.