2015-02-06 08:46
浏览 121

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)






The dummy script look the following:

  while true                             
    sleep 1
} &

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.
    解决 无用
    打赏 举报

相关推荐 更多相似问题