简体   繁体   English

并行运行Cron作业(PHP)

[英]Running Cron jobs in parallel (PHP)

In the past, I ran a bunch of scripts each as a separate cron job. 在过去,我运行了一堆脚本作为单独的cron作业。 Now I'd like to run a controller script with one cron job, then have that call the scripts separately (and in parallel, all at the same time), so I don't have to create a new cron job every time I add another script. 现在我想用一个cron作业运行一个控制器脚本,然后分别调用这些脚本(并且同时调用所有脚本),所以每次添加时我都不必创建一个新的cron作业另一个脚本。

I looked up pcntl_fork() but we don't have that installed. pcntl_fork()pcntl_fork()但我们没有安装。 Can fsockopen() do this as well? fsockopen()也可以这样做吗?

A few questions: 几个问题:

  1. I saw this example, http://phplens.com/phpeverywhere/?q=node/view/254 , that uses fsockopen() . 我在http://phplens.com/phpeverywhere/?q=node/view/254上看到了这个使用fsockopen()例子。 Will this allow me to run PHP scripts in parallel? 这是否允许我并行运行PHP脚本? Note, the scripts don't interact, but I would still like to know if any of them exited prematurely with an error. 注意,脚本不会交互,但我仍然想知道它们中的任何一个是否过早退出并出现错误。

  2. Secondly the scripts I'm running aren't externally accessible, they are internal only. 其次,我正在运行的脚本不能从外部访问,它们只是内部的。 The script was previously run like so: php -f /path/to/my/script1.php . 该脚本以前运行如下: php -f /path/to/my/script1.php It's not a web-accessible path. 它不是一个可通过Web访问的路径。 Would the example in #1 work with this, or only web-accessible paths?. #1中的示例是否适用于此,或仅适用于Web可访问的路径?

Thanks for any advice you can offer. 感谢您提供的任何建议。

You can use proc_open to run multiple processes without waiting for each process to finish. 您可以使用proc_open运行多个进程,而无需等待每个进程完成。 You will have a process handle, you can terminate each process at any time and you can read the standard output of each process. 您将拥有一个流程句柄,您可以随时终止每个流程,您可以阅读每个流程的标准输出。

You can also communicate via pipes, which is optional. 您也可以通过管道进行通信,这是可选的。

Passing 1st param php /your/path/to/script.php param1 "param2 x" means starting a separate PHP process. 传递第一个参数php /your/path/to/script.php param1 "param2 x"意味着启动一个单独的PHP进程。

proc_open (see Example #1) proc_open(参见示例#1)

Ultimately you will want to use an infinite while loop + usleep (or sleep) to avoid maxing out on the CPU. 最终,您将需要使用无限循环+ usleep(或睡眠)来避免最大化CPU。 Break when all processes finish, or after you killed them. 在所有进程完成时或在您杀死它们之后中断。

Edit: you can know if a process has exited prematurely. 编辑:您可以知道某个流程是否已过早退出。

Edit2: a simpler way of doing the above is popen Edit2:执行上述操作的简单方法是popen

Please correct me if I'm wrong, but if I understand things correctly, the solution Tiberiu-Ionut Stan proposed implies that starting the processes with proc_open and waiting for them to finish will not be run as a cron script, but is part of a running program/service, right? 如果我错了,请纠正我,但如果我理解正确,Tiberiu-Ionut Stan提出的解决方案暗示用proc_open启动进程并等待它们完成将不会作为cron脚本运行,而是一部分运行程序/服务,对吧?

As far as I understand the cron jobs, the controller script user920050 was thinking of using would be started by cron on a schedule and each new instance would launch the processes all over again, do the waiting for them to finish and probably run in parallel with other cron-launched instances of the controller script. 据我所知,cron作业,控制器脚本user920050正在考虑使用将由cron按计划启动,每个新实例将重新启动进程,等待它们完成并可能并行运行其他cron启动的控制器脚本实例。

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

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