简体   繁体   English

将exec用于长时间运行的脚本

[英]using exec for long running scripts

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, 我想从和API的一些数据,并保存在数据库中的用户,这个动作需要根据时间随机时间,有时甚至需要4个小时,我使用执行脚本exec&在后台在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, 我的问题是执行程序对于长期运行的工作是安全的,我对fork和linux进程等并不了解,所以我不知道CPU内核内部发生了什么,

Here is something I found that confused me, 我发现这让我感到困惑,

http://symcbean.blogspot.com/2010/02/php-and-long-running-processes.html 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? 有人可以告诉我我是否与exec一起朝正确的方向前进吗?

will the process be killed itself after script completion? 脚本完成后,进程会被自己杀死吗?

Thanks 谢谢

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). PHP实例启动子进程(通过exec或类似程序)。 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. 如果它不等待进程输出,则PHP脚本结束(例如,响应发送到浏览器),但是进程将处于空闲状态,等待其子进程完成。

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. 这样做的问题是,在apache决定终止其父进程(因为您有太多空闲进程)之前,并不能保证子进程(长时间运行的4小时进程)完成其工作,并且实际上杀死了其子进程。

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将您放在其前面的内容打印到标准输出(STDOUT),以便...
  • echo /usr/bin/php -q longThing.php will print to the shell /usr/bin/php -q longThing.php echo /usr/bin/php -q longThing.php将打印到外壳程序/usr/bin/php -q longThing.php
  • | (pipeline) feeds directly the STDOUT of a previous command to the standard input (STDIN) of the next command. (管道)直接将前一个命令的STDOUT馈送到下一个命令的标准输入(STDIN)。
  • at reads commands from STDIN and executes them at a specified time. at读取来自STDIN的命令并在指定的时间执行它们。 at now means the command will be executed immediately. at now意味着该命令将立即执行。

So basically this is the same thing as running the following sequence in the shell: 因此,基本上,这与在shell中运行以下序列相同:

  1. at now - Opens the at prompt at now -在提示时打开
  2. /usr/bin/php -q longThing.php - The command we want to run /usr/bin/php -q longThing.php我们要运行的命令
  3. ^D (by pressing Control+D) - To save the job ^D (按Control + D)-保存作业

So, regarding your questions: 因此,关于您的问题:

Will the child process be immediately killed after the PARENT PHP script ends? PARENT PHP脚本结束后,子进程会立即被杀死吗?

No. 没有。

Will the child process be killed at all, in some future moment? 在将来的某个时候,子进程会被杀死吗?

Yes. 是。 Apache takes care of that for you. Apache为您解决了这一问题。

Will the child process finish its job before being killed? 子进程在被杀死之前会完成工作吗?

Maybe. 也许。 Maybe not. 也许不吧。 Apache might kill it before its done. Apache可能会在完成之前将其杀死。 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. 我认为本文确实指出了正确的方向,但我不喜欢直接从PHP产生进程的想法。 In fact, PHP does not have the appropriate tools for running (long and/or intensive) bg work. 实际上,PHP没有运行bg工作(长时间和/或密集)的适当工具。 With PHP alone, you have little to no control over it. 仅凭PHP,您几乎无法控制它。

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. 我们创建了一个小程序,该程序将接受数据处理请求并将其排队(长约5分钟),并在请求完成时进行报告。 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. 该程序实际上托管在另一台LAN服务器中,从而防止了内存使用量的激增,从而降低了Web服务器的速度。

At the front-end, the user would be informed when the request was completed through long polling, 在前端,通过长时间轮询将通知用户何时完成请求,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM