dpm91915 2016-05-01 15:42
浏览 79

在exec()结束后运行后台程序之前,AJAX脚本不会响应

I run a background PHP program with exec() like this :

exec('/usr/bin/php bgScript.php "arg1" "arg2" > /dev/null 2>&1 &');

It works and the program does run in background.

Problem

I have Output Buffering Enabled and would like to keep it that way.

My whole script is this :

exec('/usr/bin/php bgScript.php "arg1" "arg2" > /dev/null 2>&1 &');
echo json_encode(array(
  "status" => "started"
));

When an AJAX request is made to the file above, the process is started and is in background. I assume this because, further requests to the server returns data and doesn't wait for the previous AJAX script to finish.

But, the problem is that the JSON data is not outputted until the background process is completed.

Since the program is made to run in the background, shouldn't the JSON Data be outputted without waiting for the exec() to end ? I don't know how to say this techinically (Forgive me) : Why does the Output Buffer continue until exec() ends ?

How can I make the script output the JSON Data right after the program is started in the background and close the connection between the AJAX script and browser ?

  • 写回答

1条回答 默认 最新

  • duandui5648 2016-05-01 15:55
    关注

    The command does not run in the background if it's stdio is not redirected. From the official documentation

    Note: 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.

    There are several methods to alter this behavior in Unix like systems you can run the command with command >/dev/null & on windows you can use start command.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答