PHP:如何使用cron(或另一种方法)让PHP脚本每秒运行一次? [英] PHP: How to use cron (or another method) to get a PHP script to run every second?

查看:788
本文介绍了PHP:如何使用cron(或另一种方法)让PHP脚本每秒运行一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个每分钟运行的cron作业,并且第一次迭代运行62秒,第二次迭代将等待开始,直到第一次出现结束,还是同时运行同一个cron作业?

If I have a cron job that runs every minute, and the first iterations runs for 62 seconds, will the second iteration wait to start until the first occurance ends, or can it happen that the same cron job runs concurrently?

要说明这一点,请参阅场景1:

To illustrate this, see scenario 1:

|0    |60   |120  |180  |240  |300
------ Cron Job 1 Taking 60 Seconds
      --------- Cron Job 2 Taking 90 Seconds
            ------ Cron Job 3 Taking 90 Seconds but overlapping with the previous cron job

,或情景2发生:

|0    |60   |120  |180  |240  |300
------ Cron Job 1 Taking 60 Seconds
      --------- Cron Job 2 Taking 90 Seconds
               XXX-------Cron Job 3 waiting til the next 1 minute interval starts

我担心的是有一个30秒的时间标记XXX,工作运行。

My concern is that there will be a period for 30 seconds marked XXX where there will be NO cron job running.

我要完成什么?我想每秒执行一个PHP脚本,无限期。它不必每秒运行一次,但它必须RUN,END,RUN,END,RUN,END。如果迭代需要5秒,它只能在第一个结束后开始下一个。但它永远不会停止。问题是,我不能确定一个迭代的长度。一次迭代可能需要1秒,或者可能需要5秒,甚至更长时间。

What am I trying to accomplish? I want to execute a PHP script every single second, indefinitely. It doesn't have to run once a second, but it must RUN, END, RUN, END, RUN, END. If an iteration takes 5 seconds, it must only start the next one after the first has ended. But it must never ever stop. The problem is that I can't determine the length of an iteration. One iteration could take 1 second, or it could take 5 seconds, or even longer.



  1. 如果没有,我如何确定PHP脚本无限运行,一个迭代一个迭代? / li>
  1. Can cron do this? So that I can be guarenteed that the script will run every single second no matter what?
  2. If not, how can I gaurentee that a PHP script runs indefinitely, one iteration after the other?


推荐答案

我最后使用 supervisord ,解决了我所有的问题。

I ended up using supervisord which solved all my problems.

这篇关于PHP:如何使用cron(或另一种方法)让PHP脚本每秒运行一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆