每 10 秒运行 cron 作业 n 次然后停止
我有一个特殊的情况,我需要查询服务以获取每天特定时间的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将“at”与“cron”结合使用。设置一个每天运行一次单个任务的 cron 作业,该任务是(目前我面前没有 UNIX 机器,因此可能会出现奇怪的语法错误)。
参数是运行计数。当 shellscript 运行时,它会重新安排自身运行,增加运行计数,因此您可以:
注意: 此解决方案还假设“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).
The argument being the run count. When the shellscript runs, it reschedules itself to run, incrementing the run count, so you could have:
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).
有多种方法可以做到这一点:
multiple ways to do this:
为什么不只让一个每天运行的 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.