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.

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.