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

查看:1088
本文介绍了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秒左右才能完成运行(转换视频,调整图像大小等)。

Some of the scripts that the queue runner script executes may take 30 seconds or so to finish running (convert video, 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("nohup $command > /dev/null 2> /dev/null & echo $! &");
?>

直接从命令行运行作业脚本,并创建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 启动您的命令。

shell_exec is not the cause of your problems. It'll wait and return the output of your command. However, you mustn't start your command in the background and using nohup.

nohup 是您的命令正在呼叫,但一旦它开始其子进程,它将立即退出,因此, shell_exec 不会等待该进程完成。您也应该禁止在背景下使用& 运行命令。

nohup is the command you are calling but as soon as it has started its child process it will exit immediatelly, thus, shell_exec will not wait for that process to be completed. You should also refrain from running the command in the background with &.

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

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