每 10 秒运行 cron 作业 n 次然后停止

发布于 2024-09-27 06:00:58 字数 517 浏览 1 评论 0原文

我有一个特殊的情况,我需要查询服务以获取每天特定时间的 n 个用户的信息。问题是,如果我一次执行所有这些操作,将使服务脱机/崩溃。

因此,为了克服这个问题,最好每 10 秒左右为 x 个用户运行一次,直到 x = n,然后停止。

我可以设置 1 个每天运行的 cron 脚本,另一个每 10 秒运行一次。例如,每日脚本会将数据库中的值设置为 1(“开始查询”)(默认值为 0 表示关闭),然后第二个脚本(每 10 秒运行一次)检查该数据库值是否为 1。设置为 true 后,它会迭代一次查询服务 x 个用户的用户,并递增同一数据库表中的另一列以跟踪它在列表中的位置。

这个解决方案的问题(据我所知)是每 10 秒运行一次的第二个脚本必须每次查询数据库以查明“开始查询”设置是否设置为 1。这可能会占用大量处理器。有人有更好的解决方案吗?

注意:代码是用 PHP 编写的 - 由于服务器上 php 脚本的最大执行时间,无法使用睡眠

我同样可以在 python 中执行此操作,cgi 脚本是否有最大执行限制?

I have a special situation where I need to query a service to get information for n users at a specific time each day. The problem is that if I do this all at once it will put the service offline / crash it.

So to overcome this it would be better to run this for x number of users every 10 seconds or so until x = n and then stop.

I could setup 1 cron script that runs daily and another that runs every 10 seconds. The daily script would set a value in the DB to 1 ('start query') for example (default would be 0 for off), where then the second script (run every 10 seconds) checks this database value for 1. Upon finding the setting set to true it then iterates through the users querying the service x users at a time and incrementing another column in the same DB table to keep track of where in the list it is at.

The problem with this solution (well according to me) is that the 2nd script that runs every 10secs has to query the DB each time to find out if the 'start query' setting is set to 1. This can be quite processor heavy. Does anyone have a better solution?

NB: Code is written in PHP - cannot use sleep due to max execution time of php scripts on server

I could equally do this in python, is there a max execution for limit on cgi scripts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瞄了个咪的 2024-10-04 06:00:59

您可以将“at”与“cron”结合使用。设置一个每天运行一次单个任务的 cron 作业,该任务是(目前我面前没有 UNIX 机器,因此可能会出现奇怪的语法错误)。

0 9 * * * at now + 10 secs /path/to/shellscript 1

参数是运行计数。当 shellscript 运行时,它会重新安排自身运行,增加运行计数,因此您可以:

#!/bin/ksh

integer i
let i=1+$1
if [[ $i -lt 11 ]]
then
    at now + 10 secs /path/to/shellscript $i

    # do everything else the script needs to do
fi

注意: 此解决方案还假设“at”可以下降到每秒一次(不确定这是否是)可能与否;可能取决于实施)。如果不是这样,这个解决方案可能从一开始就无效:-) 在这种情况下,你最好有一个脚本来完成任务并休眠(根据马特·吉布森的回答)。

You could use 'at' in combination with 'cron'. Set up a cron job that runs a single task once a day, and that task is (I don't have a UNIX box in front of me at the moment, so there may be the odd syntax error).

0 9 * * * at now + 10 secs /path/to/shellscript 1

The argument being the run count. When the shellscript runs, it reschedules itself to run, incrementing the run count, so you could have:

#!/bin/ksh

integer i
let i=1+$1
if [[ $i -lt 11 ]]
then
    at now + 10 secs /path/to/shellscript $i

    # do everything else the script needs to do
fi

NOTE: This solution also assumes 'at' can go down to per-second times (not sure if that's possible or not; may depend on the implementation). If it doesn't this solution's probably null and void from the off :-) In which case you're better off having one script that does stuff and sleeps (as per Matt Gibson's answer).

不必你懂 2024-10-04 06:00:59

有多种方法可以做到这一点:

  • 有一个运行部分作业的脚本,将所有数据放入序列化数据和状态的变量中。我已经为代码点火器实现了一个调度程序控制器,以一次获取地铁路线 http: //www.younelan.com/index.php?&repository=DCExplorer 。它可能比您所看到的更复杂,但可能会让您了解如何以不同的时间间隔运行多个任务,将它们分成更小的部分,
  • 通过 cron 或手动从命令行运行脚本 - 不会遇到超时问题

multiple ways to do this:

  • Have a script that runs part of the job, putting all data in a variable that you serialize the data and the state. I have implemented a scheduler controller for code igniter to fetch metro routes one at the time http://www.younelan.com/index.php?&repository=DCExplorer . It may be more complex than what you are looking but may give you an idea on how to have multiple tasks run at various intervals, breaking them up into smaller parts
  • run the script from the command line either through cron or manually - no suffering of timeouts
赤濁 2024-10-04 06:00:58

为什么不只让一个每天运行的 cron 脚本,每十秒调用另一个脚本,直到另一个脚本说它已完成(例如返回一个值,表示“没有更多需要处理”)?

或者一个每天运行的脚本,只是循环运行用户块,让每个 X 用户 sleep()...

请记住,当使用 CLI 版本的 PHP 您不受执行时间限制的限制,并且可以很好地与 cron 和其他 shell 脚本交互,使用标准错误和输出流等。

例如,对于第一种方法,您甚至不必使用 PHP 来进行“控制”脚本——一个非常简单的 shell 脚本,它只是循环、调用 PHP 脚本然后休眠,并检查“成功”返回值(由 PHP 脚本的 exit() 函数)就可以了。

Why not just have one cron script that runs daily, and just calls the other script every ten seconds until the other script says it's done (e.g. returns a value that says "nothing more to process")?

Or one script that runs daily and just runs through the chunks of users in a loop, sleep()ing every X users...

Bear in mind that when using the CLI version of PHP you're not constrained by execution time limits, and you can interact with cron, and other shell scripts quite nicely, using standard error and output streams, etc.

For example, for the first approach, you don't even have to use PHP for the "controlling" script -- a very simple shell script that just loops, calling your PHP script and then sleeping, and checking for a "success" return value (as returned by the PHP script's exit() function) would be fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文