drwo32555 2015-02-06 08:46
浏览 129
已采纳

PHP5,Shell_exec等待生成的linux shell任务完成

I am trying to start a linux shell script from PHP5 that will run for 24hours, but I want the webpage to return within seconds. I though this could be solved by making a script spawning of the task, but it does not seem to work.

I have been searching around for a solution or a "one shot / fire and forget" option for a couple of days without any luck.

The following example shows the problem.

In PHP 5 I make one of the following call (tried a lot it this point)

passthru("dummy_script.sh");

or

system("dummy_script.sh");

or

shell_exec("dummy_script.sh");

The dummy script look the following:

#!/bin/sh
{
  while true                             
  do 
    sleep 1
  done
} &

I can see the that process gets started, but the webpage does not return before I make a 'killall dummy_script.sh'. If I run the script manually in a terminal it return immediately and spawns of the loop.

Does anyone know a way here I can spawn of the task without making the webpage wait it ?

Hope you guys can help me out, it would be most appreciated.

  • 写回答

1条回答 默认 最新

  • douciwang6819 2015-02-06 09:15
    关注

    To answer your question:

    1. You may start looking at pcntl_fork. Or you may check this. Basically, you are using the native fork to fork the long running process so your php frontend does not have to wait.
    2. If you're feeling adventurous, you may put your "job" (your request to this long running process) in a DB. A cron job then checks the DB for incoming requests and it is the one that executes that process.
    3. Another method is to use resque, but don't bother at this point.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题