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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘