PHP shell_exec等待脚本完成? [英] PHP shell_exec wait for script to finish?

查看:329
本文介绍了PHP shell_exec等待脚本完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,该脚本查询数据库以获取要完成的作业的列表,并根据其在数据库中找到的内容(基本上是进程队列)触发其他PHP脚本.

I have a PHP script that queries a database for a list of jobs to be done and fires off other PHP scripts based on what it finds in the database (basically a process queue).

队列运行器脚本执行的某些脚本可能需要30秒钟左右才能完成运行(生成PDF,调整图像大小等).

Some of the scripts that the queue runner script executes may take 30 seconds or so to finish running (generating PDFs, resizing images, etc).

问题在于队列运行器脚本中的shell_exec()调用了处理脚本,但随后不等待它们完成,从而导致队列未完成.

The problem is that shell_exec() in the queue runner script calls the processing scripts, but then doesn't wait for them to finish, resulting in the queue not being completed.

队列运行器脚本:

#!/usr/bin/php
<?php
    // Loop through database and find jobs to be done
    shell_exec(sprintf("/root/scripts/%s.php", $row['jobName']));
?>

工作脚本:

#!/usr/bin/php
<?php
    shell_exec("/usr/bin/htmldoc -t pdf --webpage test.html > test.pdf");
    // Update database to mark job as completed
?>

直接从命令行运行作业脚本即可工作,并且创建了PDF.

Running the job script directly from the command line works and the PDF is created.

关于如何解决此问题的任何想法?还是运行进程队列的更好方法?

Any ideas on how to fix this? Or a better way to run a process queue?

推荐答案

尝试一下:

shell_exec("nohup /usr/bin/htmldoc -t pdf --webpage test.html > test.pdf 2>&1 &");

这篇关于PHP shell_exec等待脚本完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆