douyu5679 2014-11-08 22:48
浏览 128
已采纳

使用exec进行长时间运行的脚本

I want to get some data from and API and save for that user in database, this actions takes random times depending on the time and sometimes it takes even 4 hours, I am executing the script using exec and & in the background in php,

My question is that is exec safe for long running jobs, I dont know much about fork,and linux processes etc so I dont know what happened internally on CPU cores,

Here is something I found that confused me,

http://symcbean.blogspot.com/2010/02/php-and-long-running-processes.html

Can somebody tell me if I am going in right direction with exec?

will the process be killed itself after script completion?

Thanks

  • 写回答

1条回答 默认 最新

  • dsj2222222 2014-11-08 23:26
    关注

    Well, that article is talking about process "trees" and how a child process depends of it spawning parent.

    The PHP instance starts a child process (through exec or similar). If it doesn't wait for the process output, the PHP script ends (and the response is sent to the browser, for instance), but the process will sit idling, waiting for it's child process to finish.

    The problem with this is that the child process (the long running 4 hours process) is not guaranteed to finish its job, before apache decides to kill its parent process (because you have too many idle processes) and, effectively, killing its children.

    The article's author then gives the suggestion of using a daemon and separate the child process from the parent process.

    Edit:

    Answering the question you left in the comments, here's a quick explanation of the command he uses in the article

    echo /usr/bin/php -q longThing.php | at now
    

    Starting from left to right.

    • echo prints to Standard Output (STDOUT) the stuff you put in front of it so...
    • echo /usr/bin/php -q longThing.php will print to the shell /usr/bin/php -q longThing.php
    • | (pipeline) feeds directly the STDOUT of a previous command to the standard input (STDIN) of the next command.
    • at reads commands from STDIN and executes them at a specified time. at now means the command will be executed immediately.

    So basically this is the same thing as running the following sequence in the shell:

    1. at now - Opens the at prompt
    2. /usr/bin/php -q longThing.php - The command we want to run
    3. ^D (by pressing Control+D) - To save the job

    So, regarding your questions:

    Will the child process be immediately killed after the PARENT PHP script ends?

    No.

    Will the child process be killed at all, in some future moment?

    Yes. Apache takes care of that for you.

    Will the child process finish its job before being killed?

    Maybe. Maybe not. Apache might kill it before its done. Odds of that happening increase with the number of idle processes and with the time the process takes to finish.


    Sidenote:

    I think this article does point in the right direction but I dislike the idea of spawning processes directly from PHP. In fact, PHP does not have the appropriate tools for running (long and/or intensive) bg work. With PHP alone, you have little to no control over it.

    I can, however, give you the solution we found for a similar problem I faced a while ago. We created a small program that would accept and queue data processing requests (about 5 mins long) and report back when the request was finished. That way we could control how many processes could be running at the same time, memory usage, number of requests by the same user, etc...

    The program was actually hosted in another LAN server, which prevented memory usage spikes slowing down the webserver.

    At the front-end, the user would be informed when the request was completed through long polling,

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 就很莫名其妙,本来正常的Excel,突然变成了这种一格一页
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%