<p>通常,php exec函数应该阻塞,直到您运行的命令完成为止。一、 例如,PHP脚本将停止,等待命令完成,直到继续执行脚本的其余部分。我有一半的想法认为您的服务器正在经历一个max_execution_time超时,但是您已经清楚地指出,即使是几秒钟也太长,甚至这些相当短的脚本也有问题。在</p>
<p>我想到了几个解决办法。最简单的方法是修改python命令,以便a)将任何输出路由到文件或输出流,b)进程在后台运行。根据<a href="http://php.net/manual/en/function.exec.php" rel="nofollow noreferrer">docs on exec</a>:</p>
<blockquote>
<p>If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.</p>
</blockquote>
<p>我还希望您使用exec函数的两个附加可选参数。在</p>
<pre><code>$fileName = "foobar.docx";
$argVar = $fileName;
$cmd = "python3 /var/www/html/jan8/alexandrina.py /var/www/html/jan8/$argVar";
// modify your command to toss output, background the process, and output the process id
$cmd_modified = $cmd . " >/dev/null & echo \$!";
$cmd_output = NULL; // this will be an array of output
$cmd_return_value = NULL; // this will be the return value of the script
exec($cmd_modified, $cmd_output, $cmd_return_value);
echo "exec has completed</br>";
echo "output:<br>" . print_r($cmd_output, TRUE) . "<br>";
echo "return value: " . print_r($cmd_return_value, TRUE);
</code></pre>
<p>这可能有帮助,也可能没有帮助。如果没有,我们仍然可以使用posix命令来解决这个问题。在</p>
<p>编辑:根据crispytx,长脚本导致$cmd_return_val为1,这意味着发生了错误。试着改变这一行:</p>
^{pr2}$
<p>为了这个</p>
^{3}$
<p>让我们知道$cmd_output的输出是什么,它至少应该有新生成的进程的进程id。在</p>